【发布时间】:2018-08-24 18:41:35
【问题描述】:
我已使用以下tool 为提及和主题标签构建了有效的regex。我已经成功匹配了我想要插入的文本,但我需要解决以下匹配问题。
仅匹配那些以空格开头和结尾的子字符串。并且对于子字符串在字符串开头或结尾的情况 这是有效的(无论是标签还是提及),也接受它。
正则表达式找到的匹配只取不包含空格的部分,(空格只是规则的一部分,而不是 子字符串的一部分)。
我使用的正则表达式如下:(([@]{1}|[#]{1})[A-Za-z0-9]+)
字符串匹配有效和无效的一些例子:
"@hello friend" - @hello must be matched as a mention.
"@ hello friend" - here there should be no matches.
"hey@hello @hello" - here only the last @hello must be matched as a mention.
"@hello! hi @hello #hi ##hello" - here only the second @hello and #hi must be matched as a mention and hashtag respectively.
图片中的另一个示例,其中只有 "@word" 应该是有效的提及:
更新 16:35 (GMT-4) 3/15/18
我找到了解决问题的方法,在 PCRE 模式(服务器)下使用tool 并使用negative lookbehind 和negative lookahead:
(?<![^\s])(([@]{1}|[#]{1})[A-Za-z0-9]+)(?![^\s])
这里是比赛:
但现在出现了疑问,它适用于 C#? 中的正则表达式,negative lookahead 和 negative lookbehind,因为例如在 Javascript 中它不起作用,正如在工具中看到的那样,它用红线标记了我。
【问题讨论】:
-
有不同的正则表达式引擎支持不同的东西,所以在使用在线测试器时,您必须确保它使用正确的引擎,用于您使用正则表达式的语言。这也意味着一些正则表达式不能在不同的语言中重复使用。
标签: c# android regex string regex-group