Some regexp problems

L

laredotornado

Hi,

I'm using Java 6. I'm trying to write a regexp, which is failing, but
I can't figure out why (same expression works in Perl). Here is my
code ...

final Pattern addPattern = Pattern.compile("\\s*ADD\\s+([^\\s]+)\
\s*", Pattern.MULTILINE);
final Matcher addMatcher = addPattern.matcher(cellValue);

but the call "addMatcher.matches()" returns false for Strings that
look like they match to me. Here is one of them

cellValue = "ADD 6CCRASR.1 ADD 6CCRASR.2 ADD
6CCRASR.3 ADD 6CCRASR.5 ADD 6CCRASR.
10 ADD 6CCRASL.3 ADD 6CCRASL.5\nADD 6RSL.
1\nADD 6RSL.2\nADD 6RSL.3\nADD 6RSL.5\nADD 6RSL.10\nADD 6RSIT.1\nADD
6RSIT.2\nADD 6RSIT.3\nADD 6RSIT.5\nADD 6RSIT.10\nADD 6LS.5.b "

The "\n"'s are newline characters. Any idea what is wrong with the
above regexp or my code? Thanks, - Dave
 
L

Lew

laredotornado said:
I'm using Java 6. I'm trying to write a regexp, which is failing, but
I can't figure out why (same expression works in Perl). Here is my
code ...

final Pattern addPattern = Pattern.compile("\\s*ADD\\s+([^\\s]+)\
\s*", Pattern.MULTILINE);

Why do you use such exorbitant, unreadable indentation for Usenet posts? You've been around a while and really ought to know better. Do you _want_ to discourage people from answering?

Come on, man.
final Matcher addMatcher = addPattern.matcher(cellValue);

but the call "addMatcher.matches()" returns false for Strings that
look like they match to me. Here is one of them

cellValue = "ADD 6CCRASR.1 ADD 6CCRASR.2 ADD
6CCRASR.3 ADD 6CCRASR.5 ADD 6CCRASR.
10 ADD 6CCRASL.3 ADD 6CCRASL.5\nADD 6RSL.
1\nADD 6RSL.2\nADD 6RSL.3\nADD 6RSL.5\nADD 6RSL.10\nADD 6RSIT.1\nADD
6RSIT.2\nADD 6RSIT.3\nADD 6RSIT.5\nADD 6RSIT.10\nADD 6LS.5.b "

The "\n"'s are newline characters. Any idea what is wrong with the
above regexp or my code?

The indicated regex does not match that string. It only matches a substring of that string.

Check out the Javadocs for 'Matcher' for alternatives.
 
R

Roedy Green

I'm using Java 6. I'm trying to write a regexp, which is failing, but
I can't figure out why (same expression works in Perl). Here is my
code ...

see http://mindprod.com/jgloss/regex.html#MATCHINGANDFINDING

You need to understand the difference between matching, finding and
looking at.
--
Roedy Green Canadian Mind Products
http://mindprod.com
When you were a child, if you did your own experiment
to see if it was better to put to cocoa into your cup first
or the hot milk first, then you likely have the programmer gene..
 
A

Arne Vajhøj

I'm using Java 6. I'm trying to write a regexp, which is failing, but
I can't figure out why (same expression works in Perl). Here is my
code ...

final Pattern addPattern = Pattern.compile("\\s*ADD\\s+([^\\s]+)\
\s*", Pattern.MULTILINE);
final Matcher addMatcher = addPattern.matcher(cellValue);

but the call "addMatcher.matches()" returns false for Strings that
look like they match to me. Here is one of them

cellValue = "ADD 6CCRASR.1 ADD 6CCRASR.2 ADD
6CCRASR.3 ADD 6CCRASR.5 ADD 6CCRASR.
10 ADD 6CCRASL.3 ADD 6CCRASL.5\nADD 6RSL.
1\nADD 6RSL.2\nADD 6RSL.3\nADD 6RSL.5\nADD 6RSL.10\nADD 6RSIT.1\nADD
6RSIT.2\nADD 6RSIT.3\nADD 6RSIT.5\nADD 6RSIT.10\nADD 6LS.5.b "

The "\n"'s are newline characters. Any idea what is wrong with the
above regexp or my code? Thanks, - Dave

As Lew wrote then matches may not be what you want.

But I also wonder about the Pattern.MULTILINE without any
^ or $ in the pattern.

Arne
 
R

Roedy Green

I'm using Java 6. I'm trying to write a regexp, which is failing, but
I can't figure out why (same expression works in Perl). Here is my

for some general tips, see http://mindprod.com/jgloss/regex.html
--
Roedy Green Canadian Mind Products
http://mindprod.com
When you were a child, if you did your own experiment
to see if it was better to put to cocoa into your cup first
or the hot milk first, then you likely have the programmer gene..
 
R

Roedy Green

The "\n"'s are newline characters. Any idea what is wrong with the
above regexp or my code? Thanks, - Dave

I think yiou are confused between match and find.
See http://mindprod.com/jgloss/regex.html

By default, Java regexes will not scan over line endings. See above.
--
Roedy Green Canadian Mind Products
http://mindprod.com
When you were a child, if you did your own experiment
to see if it was better to put to cocoa into your cup first
or the hot milk first, then you likely have the programmer gene..
 
R

Roedy Green

The "\n"'s are newline characters. Any idea what is wrong with the
above regexp or my code? Thanks, - Dave

I think yiou are confused between match and find.
See http://mindprod.com/jgloss/regex.html

By default, Java regexes will not scan over line endings. See above.
--
Roedy Green Canadian Mind Products
http://mindprod.com
When you were a child, if you did your own experiment
to see if it was better to put to cocoa into your cup first
or the hot milk first, then you likely have the programmer gene..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top