Java String.indexOf()可以将正则表达式作为参数处理吗? [英] Can Java String.indexOf() handle a regular expression as a parameter?
本文介绍了Java String.indexOf()可以将正则表达式作为参数处理吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想捕获Java String中特定正则表达式的索引。该字符串可以用单引号或双引号括起来(有时没有引号)。如何使用Java捕获该索引?
I want to capture the index of a particular regular expression in a Java String. That String may be enclosed with single quote or double quotes (sometimes no quotes). How can I capture that index using Java?
例如:
capture String --> class = ('|"|)word('|"|)
推荐答案
否。
WorkAround:
这不是标准做法,但你可以用这个来获得结果。
WorkAround : Its not standard practice but you can get result using this.
更新:
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
}
OR
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 String.indexOf()可以将正则表达式作为参数处理吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文