0

I have this string (example):

"St 28828 En, St 217n En, St 38729mdnj dnj]spw En"

If I run this code...

Pattern patt = Pattern.compile("St(?s)(.*)En");
Matcher matc = patt.matcher(vartext);
boolean found = false;
while (matc.find()) {
    found = true;
    Log.d("LogCalc", matc.group());
} 
if (!found) {
    Log.d("LogCalc", "Nothing");
}

I get the entire string, I assume because the very first string starts at St & the very last ends at En. How can I get each individual iteration: 28828, 217n, 38729mdnj dnj]spw? I assume there is a way to have the regex pattern start from the beginning.

Thanks,

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Felipe Vidal
  • 437
  • 3
  • 15
  • 1
    https://www.regular-expressions.info/repeat.html#lazy – Pshemo Jun 28 '23 at 17:41
  • Thanks @Pshemo, with <.*?> I'm able to resolve the string covering from start to end. Can regex do the iteration as well? Or I might need to use replacement options to get them? – Felipe Vidal Jun 28 '23 at 18:03
  • 1
    "Can regex do the iteration as well?" I am not sure what you mean since you are already iterating over all matches in your code via `while (matc.find()){ ... matc.group().. }`? – Pshemo Jun 28 '23 at 18:07
  • It resolves partially, as "?" helped with ensuring I don't get the entire string from beginning to end, but only the first iteration. Right now I'm missing to know if regex can repeat the process & retrieve each individual instance by itself, or I need to iterate with a while or so. – Felipe Vidal Jun 28 '23 at 18:09
  • I see. "found = true" was breaking the iteration. Thanks!!! – Felipe Vidal Jun 28 '23 at 18:11
  • "*`found = true` was breaking the iteration*" that shouldn't be happening in your example since loop condition is based only on `matc.find()`. Also there is no `break` nor `return` based on `found`. I am guessing that you ware talking about your *real* code, not the one posted here. Anyway congrats for finding that problem, and good luck with your code :) – Pshemo Jun 28 '23 at 18:28

1 Answers1

0

"... I get the entire string, I assume because the very first string starts at St & the very last ends at En. ..."

With Matcher#group, if no argument is specified, it will return the value of the entire match—not just the capturing group.
The problem with the pattern is that it should be using a lazy-quantifier, ?.

This will match the first encountered En, as opposed to the last.

St(?s)(.*?)En

From here, to return the value of the captured group, provide the number as a value.
Group numbers start at 1—a 0 will return the entire match, similar to what you currently have.

Matcher#group (Java SE 20 & JDK 20).

matc.group(1)
Reilas
  • 3,297
  • 2
  • 4
  • 17