Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Solved: java.util.Scanner weirdness
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Solved: java.util.Scanner weirdness




On Jun 19, 2009, at 1:29 , Greg Guerin wrote:

Still, should not findInLine() in principle work in the way I expected it to? Unless I made some blatant mistake there, I'll file a bug.

I did (make a mistake, that is).


Read the source for Scanner.

Is this available? I did a quick search, but could not find it either locally nor on the web.



findInLine() eventually calls findWithinHorizon().

findWithinHorizon() is documented as using non-anchoring bounds.

"The scanner treats the horizon as a transparent, non-anchoring bound (see {@link Matcher#useTransparentBounds} and {@link Matcher#useAnchoringBounds})."

The description of Matcher.useAnchoringBounds() makes it clear that ^ and $ meta-chars are only recognized when using anchoring bounds. Therefore, findWithinHorizon() appears to not recognize ^ and $, and it does so intentionally (i.e. the behavior is documented).

Oh, I had not seen that previously.

Personally, I wouldn't use a Pattern to match a single literal like "ATOM". I'd just use readLine() on the Reader and string.startsWith(). Or readLine() and then apply an anchored Matcher to the line if the possible starting patterns were more than a single literal. Scanner appears to intentionally set the pattern to non-anchoring (search its source for useAnchoring), which doesn't seem very line-oriented to me.

After reading some more, it turns out there is another part of the docs which I had missed: because Scanner uses non-anchoring bounds, these are not matched by ^ and $, but line ends should be nonetheless.
However: "In multiline mode the expressions ^ and $ match just after or just before, respectively, a line terminator or the end of the input sequence. By default these expressions only match at the beginning and the end of the entire input sequence. "


So, I get the desired behaviour after switching to this Pattern:
Pattern atomP = Pattern.compile("^ATOM", Pattern.MULTILINE);


Thank you very much to everyone who responded!


A.

--
Ansgar Esztermann
DV-Systemadministration
Max-Planck-Institut für biophysikalische Chemie, Abteilung 105

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Re: java.util.Scanner weirdness (From: Greg Guerin <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.