简体   繁体   English

C#中匹配字符串中的!WSⅡ%^ OR WSⅡ的正则表达式

[英]Regular expression to match !WSⅡ%^ OR WSⅡ in a string in C#

How do I match alpahbet with roman symbol following with regular expressions.如何使用正则表达式将字母表与罗马符号匹配。

  1. I have got #!WSⅡ%^ WSⅡ ancient data =>(should match both WSⅡ)我有#!WSⅡ%^ WSⅡ古代数据=>(应该匹配两个WSⅡ)

  2. I have got #!WSⅡ%^ @#WSⅡ WSⅡ ancient data =>(should match 3 times)我有#!WSⅡ%^@#WSⅡWSⅡ古代数据=>(应该匹配3次)

  3. I have got AbWSⅡ WSⅡRR WSⅡ ancient data =>(should match 1 time) (AbWSⅡ WSⅡRR should not match)我有AbWSⅡ WSⅡRR WSⅡ古代数据=>(应该匹配1次)(AbWSⅡ WSⅡRR应该不匹配)

I am using the following regular expression.我正在使用以下正则表达式。

(?<![a-zA-Z0-9])(\\bWSⅡ\\b)(?=[a-zA-Z0-9]|) (?<![a-zA-Z0-9])(\\bWSⅡ\\b)(?=[a-zA-Z0-9]|)

Which is working for my all the cases except the above.除上述情况外,这适用于我的所有情况。

Note : Ⅱ is not II:Ⅱ不是Ⅱ

Please help.请帮忙。

Thanks in advance提前致谢

You can use您可以使用

(?<![^\W_])WSⅡ(?![^\W_])

See the regex demo .请参阅正则表达式演示

The regex matches正则表达式匹配

  • (?<![^\\W_]) - a negative lookbehind that matches a location that is not immediately preceded with a char other than a non-word and _ char (so it basically requires start of string or a non-word char or _ right before) (?<![^\\W_]) - 一个负向后视匹配一个位置,该位置前面不是非单词和_字符以外的字符(因此它基本上需要字符串开头或非单词字符或_就在之前)
  • WSⅡ - a WSⅡ word WSⅡ - 一个WSⅡ
  • (?![^\\W_]) - a negative lookahead that matches a location that is not immediately followed with a char other than a non-word and _ char (so it basically requires end of string or a non-word char or _ right after). (?![^\\W_]) - 匹配一个位置的负向前瞻,该位置后面没有紧跟非单词和_字符以外的字符(因此它基本上需要字符串结尾或非单词字符或_紧随其后)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM