我想在 Java 字符串中捕获特定正则表达式的索引。该字符串可以用单引号或双引号括起来(有时没有引号)。如何使用 Java 捕获该索引?
例如:
capture String --> class = ('|"|)word('|"|)
最佳答案
否。
Check source code for verification
解决方法: 它不是标准做法,但您可以使用它获得结果。
更新:
CharSequence inputStr = "abcabcab283c";
String patternStr = "[1-9]{3}";
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(inputStr);
if(matcher.find()){
System.out.println(matcher.start());//this will give you index
}
或
Regex r = new Regex("YOURREGEX");
// search for a match within a string
r.search("YOUR STRING YOUR STRING");
if(r.didMatch()){
// Prints "true" -- r.didMatch() is a boolean function
// that tells us whether the last search was successful
// in finding a pattern.
// r.left() returns left String , string before the matched pattern
int index = r.left().length();
}
关于java - Java String.indexOf() 可以将正则表达式作为参数处理吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4194310/