Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.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
Regex 使用replaceMatcheInstalling循环_Regex_Macos_Cocoa - Fatal编程技术网

Regex 使用replaceMatcheInstalling循环

Regex 使用replaceMatcheInstalling循环,regex,macos,cocoa,Regex,Macos,Cocoa,给定此字符串: *还有别的吗* 下面的代码需要replaceMatchesInString上的一个循环来转换两个匹配项,以将HTML粗体标记环绕在单词周围,否则它只匹配第一个。我不太明白为什么它在没有循环的情况下找不到第二个匹配项,因为API名称暗示它可以处理多个匹配项。我的正则表达式或标志中是否有错误 NSString * linkPattern = @"(^|\\s)\\*(.+?)\\*(\\s|$)"; NSRegularExpression * regex = [NSRegularEx

给定此字符串:

*还有别的吗*

下面的代码需要replaceMatchesInString上的一个循环来转换两个匹配项,以将HTML粗体标记环绕在单词周围,否则它只匹配第一个。我不太明白为什么它在没有循环的情况下找不到第二个匹配项,因为API名称暗示它可以处理多个匹配项。我的正则表达式或标志中是否有错误

NSString * linkPattern = @"(^|\\s)\\*(.+?)\\*(\\s|$)";
NSRegularExpression * regex = [NSRegularExpression regularExpressionWithPattern:linkPattern options:NSRegularExpressionCaseInsensitive error:&error];
while ([regex replaceMatchesInString:modifiedString options:0 range:NSMakeRange(0, [modifiedString length]) withTemplate:@"$1<b>$2</b>$3"] > 0);
NSString*linkPattern=@“(^ s)\\*(.+?)\\*(\\s\$)”;
NSRegularExpression*regex=[NSRegularExpression regular expressionwithpattern:linkPattern选项:nsregularexpressioncase不敏感错误:&error];
而([regex ReplaceMatchesInstance:modifiedString选项:0范围:NSMakerRange(0,[modifiedString长度])with Template:@“$1$2$3”]>0);

谢谢

表达式匹配字符串的开头或开头的空格,并匹配字符串结尾的空格或结尾。这意味着第一个匹配在空格中结束,第二个匹配从字符串中的下一点开始尝试:
*Else*
(即空格之后)


您应该能够使用一个非常简单的正则表达式来查找两个正则表达式之间的内容:

\*(.+?)\*
替换为:

<b>$1</b>
1美元



如果您在
*
周围寻找空间是有原因的,请在评论中告诉我,我将更新我的答案

啊!!我没有想到它会开始搜索空格后的第二个匹配项。你完全正确!空格是必需的,因为星号标记单词,因此需要忽略单词中出现的情况。您应该能够将lookarounds与OSX(非正数)一起使用,请尝试: