Objective C 正则表达式行以数字开头
Objective C Regex Line starts with number
我需要一个正则表达式字符串来查找以数字开头的行。我未来的意图是找到一个有序列表。因此,如果您也知道该怎么做,我将不胜感激。但我不知道如何找到以数字开头的行。
这是我目前所知道的。
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(^[0-9].*)" options:0 error:nil];
示例:
1. Hello
2. World
3. How's it going eh?
4. Another example string
它应该匹配所有这些字符串
要匹配多行输入,您需要指定(?m)
多行修饰符。 NSRegularExpression Class Reference:
中列出了可用的
(?ismwx-ismwx:...)
Flag settings. Evaluate the parenthesized expression with the specified flags enabled or -disabled. The flags are defined in Flag Options.
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?m)^[0-9].*" options:0 error:nil];
^^^^
此模式使 ^
和 $
在 行 的开头和结尾匹配,而不是整个字符串。
另请注意,在整个模式周围使用捕获组效率不高,因为整个匹配总是被捕获到第 1 组(例如 [match rangeAtIndex:0];
)。因此,我删除了捕获组(括号)。
我需要一个正则表达式字符串来查找以数字开头的行。我未来的意图是找到一个有序列表。因此,如果您也知道该怎么做,我将不胜感激。但我不知道如何找到以数字开头的行。
这是我目前所知道的。
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(^[0-9].*)" options:0 error:nil];
示例:
1. Hello
2. World
3. How's it going eh?
4. Another example string
它应该匹配所有这些字符串
要匹配多行输入,您需要指定(?m)
多行修饰符。 NSRegularExpression Class Reference:
(?ismwx-ismwx:...)
Flag settings. Evaluate the parenthesized expression with the specified flags enabled or -disabled. The flags are defined in Flag Options.
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?m)^[0-9].*" options:0 error:nil];
^^^^
此模式使 ^
和 $
在 行 的开头和结尾匹配,而不是整个字符串。
另请注意,在整个模式周围使用捕获组效率不高,因为整个匹配总是被捕获到第 1 组(例如 [match rangeAtIndex:0];
)。因此,我删除了捕获组(括号)。