Eric said:
Vincent wrote:
I'm not sure if there is an easy way to do this short of resetting
the matcher index, but I thought I would ask. Is it possible to
construct a regular expression so that it will return all
consecutive, 2-letter pairs? So, for example, if I had "hello
world", successive calls to matcher.find() would return:
he
el
ll
lo
wo
or
rl
ld
Thanks for any and all help.
Vincent
Matcher matcher = Pattern.compile("(?=(..))").matcher(args[0]);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
Matches non-letters, too. "(?=(\\w\\w))" comes closer, but
still doesn't get the blank (empty?) line between "lo" and "wo"
that the O.P. wants.
Ahhh, I never twigged to that stipulation...even given his example. Still,
for a single "word", where you're not worried about WS, the provided regex
does the trick.
Personally, I think Peter Duniho is on the right track: This
is a job for a simple-minded loop, not for ponderous regexes.
Depends on the spirit in which the question was asked - does the OP really
want to use a regex to solve that problem, or is he just interested in
regular expressions? The RE I provided isn't what I'd call ponderous
(leaving aside the WS issue which I wouldn't solve with regular expressions
anyway; I'd just split into words first, then process each word), but the
real reason for preferring a "simple-minded loop" in production code, to
solve this problem, is that it would take less time to *write and test* the
loop than it would take to explain the regular expression to 99% of coders.
I was mainly motivated to throw this out there because of Peter's
speculation. Given that I didn't account for the word boundaries (yet
),
an RE *can* be constructed that does produce overlapping consecutive
pairs...albeit without any matching at all.
AHS