【发布时间】:2015-01-11 18:54:00
【问题描述】:
我编写了一个小程序来在一个字符串中查找一个字符串,到目前为止它工作得很好。但我对 NSRegularExpression 有疑问 - 我需要适合我的特殊情况的 Pattern 并且卡住了。
NSString *strRegExp = [NSString stringWithFormat:@"?trunk/%@/%@/+\\([a-zA-Z0-9_\\-\\.])+/Host-1", inputstrse , inputstrsno];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:strRegExp options: NSRegularExpressionCaseInsensitive error:NULL];
NSArray *arrayOfAllMatches = [regex matchesInString:inputurl options:0 range:NSMakeRange(0, [inputurl length])];
NSRegularExpression 模式应该匹配如下字符串:
trunk/%@/%@/some-text-1/Host-1
trunk/test/1/5-text-text/Host-1
trunk/%@/%@/ 和 /Host-1 始终保持不变。只有中间的部分是可变的,并且总是如下所示:
NUMBER-Some-Text -> 5-Hello-World -> /trunk/test/1/5-hello-world/Host-1
我已经尝试过使用不同的正则表达式,如您在此处看到的:"?trunk/%@/%@/+\([a-zA-Z0-9_\-\.])+/Host-1",但我似乎仍然无法工作,也许有人可以帮助我。
当我使用以下方法构建模式时可能会出现问题:
NSString *strRegExp = [NSString stringWithFormat:@"?trunk/%@/%@/+\\([a-zA-Z0-9_\\-\\.])+/Host-1", inputstrse , inputstrsno];
然后像这样使用它:
regularExpressionWithPattern:strRegExp
我希望有人可以帮助我 - 我是正则表达式的新手。
【问题讨论】:
-
你的输入是什么样的?
-
inputse 类似于:Hello-Test-1 或 Hallo 或 Hallo-1 inputno 只是 1-30 之间的数字 inputurl 是来自网站的纯 html 代码。
-
所以你有一堆文字,想挑出看起来像
/trunk/test/1/5-hello-world/Host-1的部分? -
正确的是“5-hello-world”部分总是在变化,但看起来很相似,它也是一个数字,后跟一个“-”,后跟一些文本,可能再跟一个“-”和文本几次.
-
好的,会发布答案
标签: objective-c regex nsstring nsregularexpression