How to match “any character” in regular expression?

前端 未结 11 667
忘了有多久
忘了有多久 2020-11-27 09:23

The following should be matched:

AAA123
ABCDEFGH123
XXXX123

can I do: \".*123\" ?

11条回答
  •  情话喂你
    2020-11-27 10:17

    There are lots of sophisticated regex testing and development tools, but if you just want a simple test harness in Java, here's one for you to play with:

        String[] tests = {
            "AAA123",
            "ABCDEFGH123",
            "XXXX123",
            "XYZ123ABC",
            "123123",
            "X123",
            "123",
        };
        for (String test : tests) {
            System.out.println(test + " " +test.matches(".+123"));
        }
    

    Now you can easily add new testcases and try new patterns. Have fun exploring regex.

    See also

    • regular-expressions.info/Tutorial

提交回复
热议问题