Re: trying to create a multiple pattern matcher
- From: Albretch Mueller <lbrtchx@xxxxxxxxx>
- Date: Sat, 12 Jul 2008 19:45:56 -0400
Tom McGlynn wrote:
On Jul 9, 1:54 pm, Eric Sosman <Eric.Sos...@xxxxxxx> wrote:~Albretch Mueller wrote:[...]Quite possibly not, because regular expressions can be more
You can certainly use "((123)|(abc))" but AFAIK regexs don't tell you
if they matched the first or the second case and there is only
.replace() not a .replace(Ix) of some sort that knows that it refers to
String. regexs only tell you "I've got a match" . . .
The java regexp API surely has an internal way to know which of the
search patterns matched and it could return some index. [...]
intricate than just a simple choice between two literal strings.
For example, the regular expression "(a|b|c)(d|e|f)" matches
the strings "ad", "ae", "af", "bd", "be", "bf", "cd", "cd", "cf";
what sort of "index" distinguishes these nine cases?
What you *can* do when "((123)|(abc))" matches is to ask
the matcher which groups actually captured something. If this
regex matches, group 1 will be non-empty (of course), and so
will exactly one of groups 2 and 3.
I suppose one (i.e., Sun) could add a method to matcher
int getGroupMatchIndex(int group)
which gives the index into the matched when the matching group is an
or'ed regular expression that had a match, 0 if it matches and is not
an or'ed expression and -1 if there is no match. E.g.,
so matching xx123yy against the expression ((abc)|(123))
we'd have getGroupMatchIndex(1) == 1, getGroupMatchIndex(2) == -1 and
getGroupMatchIndex(3) == 0.
[since the groups start at group 1, not 0].
I could imagine using such a method every once in a while. I don't
think Java or Perl have anything like it though.
Thank you! You understood well the need for what I was trying to explain
Let me reinstate that I was just using a simple case to get my point across even if you actually use a true regexp, you may still need to know which one was the one that matched instead of passing the same strings and reset the matcher as many times as you need to replace some string.
I think this is not that hard specially if the transformations/replacements do not influence each other
Parsing the same string as many times as replacements need to be made is not really necessary
- Prev by Date: Re: trying to create a multiple pattern matcher
- Next by Date: Re: ObjectInputStream available() method
- Previous by thread: Re: trying to create a multiple pattern matcher
- Next by thread: Re: trying to create a multiple pattern matcher