如何在C#正则表达式中查找缩写为单词(How to find abbreviations as words in a C# regular expression)

编程入门 行业动态 更新时间:2024-10-24 01:53:57
如何在C#正则表达式中查找缩写为单词(How to find abbreviations as words in a C# regular expression)

我已经获得了一个字符串列表,可以在我的字符串中找到整个“单词”。 通常,使用\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\.))\b

In 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."

更多推荐

本文发布于:2023-07-22 12:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219939.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缩写   单词   如何在   正则表达式   find

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!