简体   繁体   中英

Building regex for special case

My text can contain values like these:-

  1. [Other]
  2. A,B,[Other]
  3. [Other][N/A]
  4. [Other]Hello
  5. [Other]Hello,baby

I want a regex expression which can satisfy only case 4 and 5. For the first three cases, it should fail. In my case, [Other] is fixed in the string value. We can simplify the regex with hardcode of [Other]. For case, event text [N/A] is fixed, so that makes it different from case 4 and 5.

  • The string must start with word [Other]. The string must not contain word [N/A]. The string must not have word [Other] alone.

Pls help and suggest the regex expression.

For testing, pls use http://www.gskinner.com/RegExr/

试试这个regex

^\[[^\]]+\][a-zA-Z,]+$

Depends what exactly you want to match, but this works for your case

^\[Other\][^\[]+$

Which breaks down like... start ( ^ ) followed by the string [Other] (escaped) followed by not [ ( [^\\[] ) one or more times ( + ) followed by end ( $ )


EDIT Or a more specific version (as per comment) is as above, but second half has not [ followed by not N followed by not \\ optionally, followed by not A optionally, followed by not ] followed by zero or more of any character ( .* ) end ( $ )

^\[Other\][^\[][^\N]?[^\\]?[^A]?[^\]]?.*$

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