So I have a little problem
Here's my text
AFTER_2011/03/01 GREATER_2004_NOT
I want AFTER and GREATER, so i have the following regex:
[A-Z]{2,}\\B
At first its okay and I get AFTER, but the second time i get the entire line. i tried add '?' before and after to turn it reluctant, also i tried \G to turn it global. nothing works for me. in other times i used regex it has automatically started searching where last stopped, but not this time. any suggestions?
Edit: So here is the code i wrote:
private void checkFilterNames(String[] sections){
_validityPatt = Pattern.compile("[A-Z]{2,}\\B");
boolean foundName;
for (int i=0; i<sections.length; i++){
_checker = _validityPatt.matcher(sections[i]);
_checker.find();
String currentName = sections[i].substring(1, _check开发者_JAVA技巧er.end());
while (!currentName.equals("ACTION")){
foundName = false;
System.out.println("checking "+currentName);
for (int k=0; k<FilterManager.getNames().length; k++){
if (currentName.equals(FilterManager.getNames()[k])){
foundName = true;
break;
}
}
if (!foundName){
System.out.println("no such FILTER/ACTION/ORDER "+currentName);
System.exit(-1);
}
_checker.find();
currentName = sections[i].substring(1, _checker.end());
}
}
So this is the code. I want to isolate AFTER and then GREATER. I had a little mistake before the first loop i get 'AFTER' and then i get 'AFTER_2011/03/01 GREATER'
currentName = sections[i].substring(1, _checker.end());
should be:
currentName = sections[i].substring(_checker.start(), _checker.end());
I tested your regex, it is fine. I think the reason you are getting the wrong string is because you're always getting substring from index of 1.
First iteration (assuming 1 based index):
- start==1, end==6 .substring(1, end)
- is 'AFTER' so you're good
Second iteration
- start==18, end==25
- .substring(1, end) is 'AFTER_2011/03/01 GREATER' so end index is good, but start index is not
- .substring(start, end) is 'GREATER', this is what you want I believe
Not very clear. You could try a regex like this
([A-Z]{2,})_
精彩评论