我已经获得了一个字符串列表,可以在我的字符串中找到整个“单词”。 通常,使用\b锚点适用于大多数事情,除非我试图将&字符作为单词找到或者缩写后面有一个点,因为\b在空格和&字符之间不匹配,或经过一段时间和空间。
例如,找到这些字符串:
& b&w bpi p.我正在尝试写一些类似的东西:
\b((&)|(b&w)|(bpi)|(p\.))\b在测试字符串中:
my b&w and & and p. test.我也尝试使用\s来检查空格,但我不想捕获空白,我也无法弄清楚如何不这样做。 然后它还需要检查字符串的开头和结尾,我相信。
I have been given a list of strings to find as whole "words" in my string. Generally, using the \b anchor works for most things except when I'm trying to find the & character as a word or if the abbreviation has a dot after it since the \b doesn't match between the space and the & character, or after a period and space.
For instance to find these strings:
& b&w bpi p.I'm trying to write something like:
\b((&)|(b&w)|(bpi)|(p\.))\bIn a test string:
my b&w and & and p. test.I've also tried using \s to check for whitespace but I don't want to capture the whitespace and I haven't been able to figure out how not to. It would also then need to check for beginning and ending of the string as well I believe.
最满意答案
而不是使用单词边界(\ b),您可以使用查找(空格)OR ^开头或行结尾的断言...如下所示:
(?<=^|\s)([^\s]*)(?=\s|$)工作正则表达式示例:
http://regex101.com/r/rJ0wU4
测试字符串:
my b&w and & and p. test.火柴:
"my", "b&w", "and", "&", "and", "p.", "test."Instead of using word boundaries (\b) you could use look around assertions for (space) OR ^beginning or $end of line.. like so:
(?<=^|\s)([^\s]*)(?=\s|$)Working regex example:
http://regex101.com/r/rJ0wU4
Test string:
my b&w and & and p. test.Matches:
"my", "b&w", "and", "&", "and", "p.", "test."更多推荐
发布评论