【问题标题】:regular expression in java matcher patternjava匹配器模式中的正则表达式
【发布时间】:2015-01-31 11:18:50
【问题描述】:

想问一下Matcher&Pattern中的java正则表达式

我的代码是

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

我正在寻找 rgx 的

+TR
"TR
+TM
"TM

我的正则表达式有问题。有人可以指出吗?非常感谢

【问题讨论】:

    标签: java regex pattern-matching


    【解决方案1】:

    对于您的情况,以下内容就足够了。

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

    DEMO

    [+\"] - 匹配双引号或+ 符号的字符类。

    [MR] - 匹配 MR 字符。

    \" - 匹配文字双引号。

    示例:

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

    输出:

    true
    true
    true
    true
    

    【讨论】:

    • 谢谢,虽然我需要逃离 [ 和 ]。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    • 2016-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多