Skip to content

查找特定字符串

列举出多种正则表达式,输入不同的字符串,输出匹配结果。

尖括号<<>>匹配示例

测试用的代码,正则表达式涉及到尖括号。

    public static void main(String[] args) {
        final String[] regs = {"<<>>", "<<.>>", "<<*>>", "<<?>>", "<<.*?>>", ".*<<.*?>>.*"};
        final String[] strs = {"<<>>", "<<abc>>", "abc>>", "<<abc", "<<" ,">>", "+;<<hello>>", "+;<<hello>>:'", "<<hello>>..."};
        for (String s : strs) {
            for (String r :regs) {
                findMatch(r, s);
            }
            System.out.println();
        }
    }

    private static void findMatch(String reg, String text) {
        System.out.println(text + " matches " + reg + " : " + text.matches(reg));
    }

运行结果

<<>> matches <<>> : true
<<>> matches <<.>> : false
<<>> matches <<*>> : true
<<>> matches <<?>> : true
<<>> matches <<.*?>> : true
<<>> matches .*<<.*?>>.* : true

<<abc>> matches <<>> : false
<<abc>> matches <<.>> : false
<<abc>> matches <<*>> : false
<<abc>> matches <<?>> : false
<<abc>> matches <<.*?>> : true
<<abc>> matches .*<<.*?>>.* : true

abc>> matches <<>> : false
abc>> matches <<.>> : false
abc>> matches <<*>> : false
abc>> matches <<?>> : false
abc>> matches <<.*?>> : false
abc>> matches .*<<.*?>>.* : false

<<abc matches <<>> : false
<<abc matches <<.>> : false
<<abc matches <<*>> : false
<<abc matches <<?>> : false
<<abc matches <<.*?>> : false
<<abc matches .*<<.*?>>.* : false

<< matches <<>> : false
<< matches <<.>> : false
<< matches <<*>> : false
<< matches <<?>> : false
<< matches <<.*?>> : false
<< matches .*<<.*?>>.* : false

>> matches <<>> : false
>> matches <<.>> : false
>> matches <<*>> : false
>> matches <<?>> : false
>> matches <<.*?>> : false
>> matches .*<<.*?>>.* : false

+;<<hello>> matches <<>> : false
+;<<hello>> matches <<.>> : false
+;<<hello>> matches <<*>> : false
+;<<hello>> matches <<?>> : false
+;<<hello>> matches <<.*?>> : false
+;<<hello>> matches .*<<.*?>>.* : true

+;<<hello>>:' matches <<>> : false
+;<<hello>>:' matches <<.>> : false
+;<<hello>>:' matches <<*>> : false
+;<<hello>>:' matches <<?>> : false
+;<<hello>>:' matches <<.*?>> : false
+;<<hello>>:' matches .*<<.*?>>.* : true

<<hello>>... matches <<>> : false
<<hello>>... matches <<.>> : false
<<hello>>... matches <<*>> : false
<<hello>>... matches <<?>> : false
<<hello>>... matches <<.*?>> : false
<<hello>>... matches .*<<.*?>>.* : true

非字母中间找到英文单词

demo