1. Test whether the given regular expression matches the input string, where the regular expression is used only once
Private String regex; Private String input; @Before publicvoid init () { regex = "\\[{0,1} ([\u4e00-\u9fa5]+)/[a-za-z]+\\]{ 0,1}[a-za-z]* ";
input = "Yan ' an/ns"; }
/** * Tests if the given regular expression matches the input string, * Here the regular expression is used only once */ @Test public void test1matches () { boolean ismacher = pattern.matches (regex, input); System.out.println (Ismacher); }
Test Result: True
2. Get a matching part of the string from the matching regular expression. If you get the contents of the first parenthesis in the matching expression:
String regex = "\\[{0,1} ([\u4e00-\u9fa5]+)/[a-za-z]+\\]{0,1}[a-za-z]*"; string input = "[China/ns State Council/nt]nt" ; = Pattern.compile (regex); = Pattern.matcher (input); while (Matcher.find ()) { // count starting from 1 System.out.println (matcher.group (1) ); }
Test results:
China
State council
Java.util Pattern and Mathcer