简体   繁体   中英

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Ⅱ)

  2. I have got #!WSⅡ%^ @#WSⅡ WSⅡ ancient data =>(should match 3 times)

  3. I have got AbWSⅡ WSⅡRR WSⅡ ancient data =>(should match 1 time) (AbWSⅡ WSⅡRR should not match)

I am using the following regular expression.

(?<![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)
  • WSⅡ - a WSⅡ word
  • (?![^\\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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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