我想获取来自 Matcher
的匹配字符串的一部分,像这样:
Pattern pat = Pattern.compile("a.*l.*z");
Matcher match = pat.matcher("abcdlmnoz"); // I'd want to get bcd AND mno
ArrayList<String> values = match.magic(); //here is where your magic happens =)
ArrayList<String>
仅针对此示例,我很高兴收到列表或单个字符串项目。最好的是什么.htaccess
文件和 RewriteRule
的做法:
RewriteRule (.*)/path?(.*) $1/$2/modified-path/
好吧,将那些 (.*) 放入 $arguments 中就像 ArrayList
一样酷或访问String
分别地。我一直在 Java Matcher API 寻找东西,但我并没有看到里面有什么有用的东西。
提前谢谢各位。
最佳答案
您可以使用 (_)
捕获正则表达式匹配中的组:
Pattern pat = Pattern.compile("a(.*)l(.*)z");
boolean b = match.matches(); // don't forget to attempt the match
然后使用 match.group(n)获取捕获的该部分。这些组存储在 match
对象中。
关于java - 从匹配器获取字符串的特定部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9387541/