简体   繁体   中英

Paired characters in regular expression

I expect this is very easy, but I can't work out how to match optional character pairs in regex. Regular expressions are not something I have ever had to do before.

I want to be able to match "=N","=B","=R" or "=Q" in a character string, optionally -- but if they appear, they must appear paired with the equal sign. So =?[NBRQ]? won't work for me, because someone could type 'N' without the accompanying equal sign. So it must be "=N","=B", "=R" or "=Q" or nothing at all.

If you need to make more than one regex production optional, enclose them in parentheses, capturing or non-capturing:

(=[NBRQ])?

The above would match an optional =N , =B , =R , or =Q . Since the question mark appears after parentheses, the entire group is optional, not its individual parts.

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