简体   繁体   English

Java Matcher模式中的正则表达式

[英]regular expression in java matcher pattern

I would like to ask the java regular expression in Matcher & Pattern 我想问Matcher&Pattern中的Java正则表达式

my code is 我的代码是

Pattern TR = Pattern.compile("\\\\[\+"\\\\]T\\\\[MR\\\\]");

I'm looking for the rgx for 我正在寻找rgx

+TR
"TR
+TM
"TM

There is something wrong on my regular expression. 我的正则表达式有问题。 Could someone please point out ? 有人可以指出吗? Thank you so much 非常感谢

Just the below would be enough for your case. 仅以下内容就足以满足您的要求。

Pattern TR = Pattern.compile("[+\"]T[MR]");

DEMO 演示

[+\\"] - Character class which matches double quotes or + symbol. [+\\"] -匹配双引号或+符号的字符类。

[MR] - Matches M or R character. [MR] -匹配MR字符。

\\" - Matches the literal double quotes. \\" -匹配文字双引号。

Example: 例:

String[] s = {"+TR","\"TR","+TM","\"TM"};
for (String i:s)
{
 System.out.println(i.matches("[+\"]T[MR]"));
}

Output: 输出:

true
true
true
true

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

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