NSRegularExpression 用于从字符串中检索数字
我使用 NSScanner 从下面的文本中检索数字。但有时结果并不像预期的那样。听说 iOS 4 中提供的 NSRegularExpression 类更适合进行这种类型的提取。由于我是 NSRegularExpression 的初学者,我发现 Apple 提供的文档很难理解。任何帮助将不胜感激。谢谢。
输入:
1D03 04 10 17 47 D24--
输出:
03 04 10 17 47 24
前5组数字应小于59,最后一组数字应小于39。
I had used NSScanner for retrieving numbers from below text. But sometimes the result is not like what is expected. Heard that NSRegularExpression class which is available fro iOS 4 is better to do this type of extraction. As I am a beginner on NSRegularExpression, I found the documentation provided by Apple is difficult to understand. Any help will be greatly appreciated. Thanks.
Input:
1D03 04 10 17 47 D24--
Output:
03 04 10 17 47 24
The first 5 sets of numbers should be less than 59 and last set should be less than 39.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个..
try this ..
或者这样:
此正则表达式匹配以 0 个或多个非空格字符 (\S) 开头和结尾且中间有两位数字 (\d) 的所有内容。匹配项将替换为字符串中间的两个数字。
Or this:
This Regular Expression matches everything which starts and ends with 0 or more non withspace Charcters (\S) and has in the middle two digits (\d). The matches are replaced by the tow digits in the middle of the string.