我嘗試使用正則表達式解析字符串以從中獲取參數。 作爲一個例子:使用正則表達式的Java Stringparsing
String: "TestStringpart1 with second test part2" Result should be: String[] {"part1", "part2"} Regexp: "TestString(.*?) with second test (.*?)"
我Testcode是:
String regexp = "TestString(.*?) with second test (.*?)"; String res = "TestStringpart1 with second test part2"; Pattern pattern = Pattern.compile(regexp); Matcher matcher = pattern.matcher(res); int i = 0; while(matcher.find()) { i++; System.out.println(matcher.group(i)); }
但只輸出 「第一部分」 可能有人給我暗示?
感謝
您可以使用以下網站來檢查您正則表達式對測試用例:https://regex101.com/ – luizfzs 2017-05-26 19:52:29