Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > java.util.regex ^ problem

Reply
Thread Tools

java.util.regex ^ problem

 
 
Glenn Meter
Guest
Posts: n/a
 
      09-21-2004
I'm trying to use the regex package to match strings at the beginning
of lines within files. However, it looks like ^ is only matching on
items at the start of the string, not at the start of a line.

This sets b to true, with m.start() returning 0 [the beginning of text]:

Pattern p = Pattern.compile("^foo");
Matcher m = p.matcher("foo\nmatch bar\n");
boolean b = m.find();

This leaves b as false:

Pattern p = Pattern.compile("^match");
Matcher m = p.matcher("foo\nmatch bar\n");
boolean b = m.find();

Since the top test succeeds, it looks like it's taking '^' as a
boundary marker instead of the "not this character" marker.
The fact that RegexTestHarness only checks one line at a time isn't
giving me great confidence that multi-line input was thoroughly tested.

Any clues would be greatly appreciated.

Thanks,
Glenn

 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      09-21-2004
Glenn Meter wrote:
> I'm trying to use the regex package to match strings at the beginning
> of lines within files. However, it looks like ^ is only matching on
> items at the start of the string, not at the start of a line.


From the API documentation for java.util.Pattern:

By default, the regular expressions ^ and $ ignore line terminators
and only match at the beginning and the end, respectively, of the
entire input sequence. If MULTILINE mode is activated then these
expressions match just after or just before, respectively, a line
terminator or the end of the input sequence with the exception that
the expression ^ never matches at the end of input, even if the last
character is a newline.

So try:

Pattern p = Pattern.compile("^match", Pattern.MULTILINE);
Matcher m = p.matcher("foo\nmatch bar\n");
boolean b = m.find();

I haven't tried it, but I suspect it will work.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
 
 
 
Glenn Meter
Guest
Posts: n/a
 
      09-21-2004
That did the trick. Next time I'll RTFM more carefully.

Thanks,
Glenn


On 2004-09-20 23:43:48 -0500, Chris Smith <(E-Mail Removed)> said:

> From the API documentation for java.util.Pattern:
>
> By default, the regular expressions ^ and $ ignore line terminators
> and only match at the beginning and the end, respectively, of the
> entire input sequence. If MULTILINE mode is activated then these
> expressions match just after or just before, respectively, a line
> terminator or the end of the input sequence with the exception that
> the expression ^ never matches at the end of input, even if the last
> character is a newline.
>
> So try:
>
> Pattern p = Pattern.compile("^match", Pattern.MULTILINE);
> Matcher m = p.matcher("foo\nmatch bar\n");
> boolean b = m.find();
>
> I haven't tried it, but I suspect it will work.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem problem problem :( Need Help Mike ASP General 2 05-11-2004 08:36 AM



Advertisments