android - Java Regex Extract String between two Strings -
the problem
i have pattern "nom ? oscars" or "nom 2 golden globes. nom ? oscars. 30 wins 18 nominations" , want determine ? regex, amount of oscars.
what tried
it seems there
corresponding questions: extract string between 2 strings in java ,
how find value between 2 strings? tried pattern:
final pattern pattern = pattern.compile("for(.*?)oscar");
next tried following question: java - best way grab strings between 2 strings? (regex?)
final pattern pattern = pattern.compile(pattern.quote("nom") +"(.*?)"+ pattern.quote("oscar"));
the rest of code:
final matcher matcher = pattern.matcher("nom 3 oscar"); while(matcher.find()){ } log.d("test", matcher.group(1));
all of these pattern result in exception:
java.lang.illegalstateexception: no successful match far
i think oversee simple.
can guys me ?
edit
so problem call matcher.group(1) after loop. missunderstood working of find method. code working indeed, when call matcher.group(1) inside loop.
final pattern pattern = pattern.compile("for(.*?)oscar"); final matcher matcher = pattern.matcher("nom 3 oscar"); while(matcher.find()){ log.d("test", matcher.group(1)); }
i write test in extract string between 2 strings in java , working. think input string don't matches:
@test public void regex() { string str = "nom 3 oscar, dom 234235 oscars"; pattern pattern = pattern.compile("for(.*?)oscar"); matcher matcher = pattern.matcher(str); while (matcher.find()) { system.out.println(matcher.group(1)); } }
output:
3 234235
after answer edited question , see, in input string "oscar" starts lowecase "o", in pattern uppercase "o".
Comments
Post a Comment