[PJUG Javamail] Silly Regex Question
Sean Adkinson
sean.adkinson at gmail.com
Tue Jun 23 13:32:55 EDT 2009
Yep, I caught what Siraj caught.
Your code calls "m.find()" three total times, the first two returning true.
Here are the three calls:
if(m.find() && toggle == 0) // true && true
if(m.find() && toggle == 0) // true && false
else if(m.find() && toggle == 1) // false && true
What you really want is this:
for(int i = 0; i < foo.length; i++) {
boolean found = m.find();
Matcher m = langs.matcher(foo[i]);
if(found && toggle == 0) { //we have a language, but it's the first
one
toggle = 1;
languages[0] = fetchLanguageName(foo[i]);
}
else if(found && toggle == 1) { //the second language we find
toggle = 2;
languages[1] = fetchLanguageName(foo[i]);
}
}
- Sean
On Tue, Jun 23, 2009 at 10:20 AM, Siraj Podikunju <sirajp at spconsult.com>wrote:
> Richard,
>
> According to the javadocs, find() looks for the next match each time it
> is called unless Matcher is reset. So in your code, the second call to
> m.find() in the "else if" condition would return false.
>
> But as Zack posted, contains() is a simpler solution in this context.
>
> BTW: RegexBuddy (regexbuddy.com) is a great help in debugging regular
> expressions. No affiliation - just a useful tool in my dev kit.
>
> Siraj.
>
> Richard Johnson wrote:
> > Hi all,
> >
> > What's wrong with my logic and/or regex?
> >
> >
> > I'm parsing a text file, extracting hopefully useful information from
> > a stated directory path. I have a java pattern/regex that looks like
> > this:
> >
> > String[] languages = new String[2];
> > String[] foo = inputLine.split("\\\\");
> > Pattern langs = Pattern.compile("_TM");
> > int toggle = 0;
> > for(int i = 0; i < foo.length; i++) {
> > Matcher m = langs.matcher(foo[i]);
> > if(m.find() && toggle == 0) { //we have a language, but it's the
> > first one
> > toggle = 1;
> > languages[0] = fetchLanguageName(foo[i]);
> > }
> > else if(m.find() && toggle == 1) { //the second language we find
> > toggle = 2;
> > languages[1] = fetchLanguageName(foo[i]);
> > }
> > }
> > Given an input line like this:
> > Translation Memory:
> >
> X:\2Checkout\2Checkout_English(U.S.)_TM\2Checkout_Chinese(Traditional)_TM\cht_2CheckOut.tmw
> >
> > It quite happily finds the first occurrence of '_TM' but then fails to
> > trigger on the second. (i.e. we find English but not Chinese.)
> >
> > --
> >
> > Richard Johnson, Systems Architect
> >
> > Lingo Systems
> >
> > 15115 SW Sequoia Pkwy #200
> >
> > Portland Oregon 97224
> >
> >
> >
> > richardj at lingosys.com <mailto:richardj at lingosys.com>
> >
> > http://www.lingosys.com <http://www.lingosys.com/>
> >
> > voice 503-419-4889 or 800-878-8523
> >
> > FAX 503-419-4873
> >
> >
> >
>
> --
> Siraj Podikunju
> SP Consulting, Inc.
> Tel: 360-600-6608
> Fax: 360-260-3448
> E-Mail: sirajp at spconsult.com
>
> _______________________________________________
> Web Site - http://www.pjug.org/
> Javamail mailing list
> Javamail at pjug.org
> http://www.pjug.org/mailman/listinfo/javamail
>
--
Sean Adkinson
(503) 731-5488 work, (503) 866-0852 cell
sean.adkinson at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.pjug.org/pipermail/javamail/attachments/20090623/e0ce4f16/attachment.html
More information about the Javamail
mailing list