Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #13315 > unrolled thread

Some regexp problems

Started bylaredotornado <laredotornado@zipmail.com>
First post2012-04-02 12:32 -0700
Last post2012-04-03 16:29 -0700
Articles 8 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Some regexp problems laredotornado <laredotornado@zipmail.com> - 2012-04-02 12:32 -0700
    Re: Some regexp problems Lew <lewbloch@gmail.com> - 2012-04-02 12:48 -0700
    Re: Some regexp problems Roedy Green <see_website@mindprod.com.invalid> - 2012-04-02 16:07 -0700
    Re: Some regexp problems Arne Vajhøj <arne@vajhoej.dk> - 2012-04-02 19:49 -0400
    Re: Some regexp problems Roedy Green <see_website@mindprod.com.invalid> - 2012-04-03 10:47 -0700
    Re: Some regexp problems Roedy Green <see_website@mindprod.com.invalid> - 2012-04-03 10:48 -0700
      Re: Some regexp problems Arne Vajhøj <arne@vajhoej.dk> - 2012-04-03 19:30 -0400
    Re: Some regexp problems Roedy Green <see_website@mindprod.com.invalid> - 2012-04-03 16:29 -0700

#13315 — Some regexp problems

Fromlaredotornado <laredotornado@zipmail.com>
Date2012-04-02 12:32 -0700
SubjectSome regexp problems
Message-ID<2048846a-7678-4d0e-b76f-38670934297d@m16g2000yqc.googlegroups.com>
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

[toc] | [next] | [standalone]


#13317

FromLew <lewbloch@gmail.com>
Date2012-04-02 12:48 -0700
Message-ID<13964491.992.1333396119359.JavaMail.geo-discussion-forums@pbgr6>
In reply to#13315
laredotornado wrote:
> 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.

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#13326

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-04-02 16:07 -0700
Message-ID<97ckn71a4q66i16k22hfro2a2r7pe59qai@4ax.com>
In reply to#13315
On Mon, 2 Apr 2012 12:32:13 -0700 (PDT), laredotornado
<laredotornado@zipmail.com> wrote, quoted or indirectly quoted someone
who 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 ...

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..

[toc] | [prev] | [next] | [standalone]


#13329

FromArne Vajhøj <arne@vajhoej.dk>
Date2012-04-02 19:49 -0400
Message-ID<4f7a3afd$0$295$14726298@news.sunsite.dk>
In reply to#13315
On 4/2/2012 3:32 PM, laredotornado wrote:
> 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

[toc] | [prev] | [next] | [standalone]


#13349

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-04-03 10:47 -0700
Message-ID<csdmn7tdjvmqm3q22qi22oeavr4s9mcgl0@4ax.com>
In reply to#13315
On Mon, 2 Apr 2012 12:32:13 -0700 (PDT), laredotornado
<laredotornado@zipmail.com> wrote, quoted or indirectly quoted someone
who 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

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..

[toc] | [prev] | [next] | [standalone]


#13350

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-04-03 10:48 -0700
Message-ID<ntdmn75o3n0rlck6h2671fqk42r5inv53k@4ax.com>
In reply to#13315
On Mon, 2 Apr 2012 12:32:13 -0700 (PDT), laredotornado
<laredotornado@zipmail.com> wrote, quoted or indirectly quoted someone
who said :

>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..

[toc] | [prev] | [next] | [standalone]


#13357

FromArne Vajhøj <arne@vajhoej.dk>
Date2012-04-03 19:30 -0400
Message-ID<4f7b8810$0$286$14726298@news.sunsite.dk>
In reply to#13350
On 4/3/2012 1:48 PM, Roedy Green wrote:
> By default, Java regexes will not scan over line endings. See above.

Sure they will.

(just note that . will not match newlines by default)

Arne

[toc] | [prev] | [next] | [standalone]


#13356

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-04-03 16:29 -0700
Message-ID<gu1nn79p3dqht04lai8cm8f1pgj86v4brq@4ax.com>
In reply to#13315
On Mon, 2 Apr 2012 12:32:13 -0700 (PDT), laredotornado
<laredotornado@zipmail.com> wrote, quoted or indirectly quoted someone
who said :

>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..

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web