[PJUG Javamail] Silly Regex Question
Siraj Podikunju
sirajp at spconsult.com
Tue Jun 23 13:20:25 EDT 2009
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
More information about the Javamail
mailing list