Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone NSRegularExpression:如何匹配以开头的单词@_Iphone_Objective C - Fatal编程技术网

Iphone NSRegularExpression:如何匹配以开头的单词@

Iphone NSRegularExpression:如何匹配以开头的单词@,iphone,objective-c,Iphone,Objective C,我想将对用户名的引用,如@me、@you、@theme(推特风格)转换为链接。对此,正确的NSRegularExpression模式是什么 根据下面答案的输入,以下是我最终使用的 NSError *error = NULL; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?<!\\w)@([\\w\\._-]+)?" options:NSRegularExpression

我想将对用户名的引用,如@me、@you、@theme(推特风格)转换为链接。对此,正确的NSRegularExpression模式是什么


根据下面答案的输入,以下是我最终使用的

NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?<!\\w)@([\\w\\._-]+)?" options:NSRegularExpressionCaseInsensitive error:&error];
NSString *newString = [regex stringByReplacingMatchesInString:stringIn options:0 range:NSMakeRange(0, [stringIn length]) withTemplate:@"<a href='http://mydomain.com/$1'>$0</a>"];
NSError*error=NULL;

NSRegularExpression*regex=[NSRegularExpression regular expression with pattern:@”(?
@[a-zA-Z0-9\]+
应该可以很好地完成这项工作

我建议您输入一些测试数据并修改正则表达式,以精确匹配您需要的数据。

(?应该是非常安全的


(?被称为a,确保@前面没有单词字符,以防止与电子邮件地址匹配。

不必转义和号。Twitter名称也可能包含下划线。此外,星号表示零次或多次出现,因此它本身将与
@
匹配。@dreamlax很好。我不好。@Nikolai我希望希望使用正则表达式的人至少对它们有足够的了解,可以对它们进行一些小的修改。无论如何,答案会更新以反映这两种批评。效果很好。正如E-madd在他的编辑中指出的,如果你在Twitter上使用正则表达式,你想禁止句号和连字符,所以模式是“(?)?