简体   繁体   English

正则表达式中的成对字符

[英]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. 我希望能够匹配字符串中的“ = N”,“ = B”,“ = R”或“ = Q”-但是,如果它们出现,它们必须与等号配对出现。 So =?[NBRQ]? == [[NBRQ]? won't work for me, because someone could type 'N' without the accompanying equal sign. 不会为我工作,因为有人可以在没有伴随的等号的情况下键入“ N”。 So it must be "=N","=B", "=R" or "=Q" or nothing at all. 因此它必须是“ = N”,“ = B”,“ = R”或“ = Q”或什么都没有。

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 . 上面将匹配可选的=N=B=R=Q Since the question mark appears after parentheses, the entire group is optional, not its individual parts. 由于问号出现在括号后面,因此整个组是可选的,而不是其各个部分。

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

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