NSRegularExpression 用于从字符串中检索数字

发布于 2024-12-09 09:14:06 字数 329 浏览 0 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟雨凡馨 2024-12-16 09:14:06

试试这个..

NSString *originalString = @"1D03 04 10 17 47 D24---";

NSLog(@"%@", originalString);
NSMutableString *strippedString = [NSMutableString 
                                   stringWithCapacity:originalString.length];

NSScanner *scanner = [NSScanner scannerWithString:originalString];
NSCharacterSet *numbers = [NSCharacterSet 
                           characterSetWithCharactersInString:@"0123456789  "];

while ([scanner isAtEnd] == NO) {
    NSString *buffer;
    if ([scanner scanCharactersFromSet:numbers intoString:&buffer]) {
        [strippedString appendString:buffer];

    } else {
        [scanner setScanLocation:([scanner scanLocation] + 1)];
    }
}

NSLog(@"%@", strippedString);

try this ..

NSString *originalString = @"1D03 04 10 17 47 D24---";

NSLog(@"%@", originalString);
NSMutableString *strippedString = [NSMutableString 
                                   stringWithCapacity:originalString.length];

NSScanner *scanner = [NSScanner scannerWithString:originalString];
NSCharacterSet *numbers = [NSCharacterSet 
                           characterSetWithCharactersInString:@"0123456789  "];

while ([scanner isAtEnd] == NO) {
    NSString *buffer;
    if ([scanner scanCharactersFromSet:numbers intoString:&buffer]) {
        [strippedString appendString:buffer];

    } else {
        [scanner setScanLocation:([scanner scanLocation] + 1)];
    }
}

NSLog(@"%@", strippedString);
傲性难收 2024-12-16 09:14:06

或者这样:

NSString *string = @"1D03 04 10 17 47 D24---";

NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\S*(\\d{2})\\S*"
                                                                       options:NSRegularExpressionCaseInsensitive
                                                                         error:&error];


NSString *modifiedString = [regex stringByReplacingMatchesInString:string
                                                           options:0
                                                             range:NSMakeRange(0, [string length])
                                                      withTemplate:@"$1"];

此正则表达式匹配以 0 个或多个非空格字符 (\S) 开头和结尾且中间有两位数字 (\d) 的所有内容。匹配项将替换为字符串中间的两个数字。

Or this:

NSString *string = @"1D03 04 10 17 47 D24---";

NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\S*(\\d{2})\\S*"
                                                                       options:NSRegularExpressionCaseInsensitive
                                                                         error:&error];


NSString *modifiedString = [regex stringByReplacingMatchesInString:string
                                                           options:0
                                                             range:NSMakeRange(0, [string length])
                                                      withTemplate:@"$1"];

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文