Trouble with regular expressions

L

laredotornado

Hi,

I was hoping someone could help me with a simple regular expression
dilemma. I'm using Java 1.5. I have this code:

String regex = accountid; // value is
9998698989898
System.out.println("regexp:" + regex);
Pattern p1 = Pattern.compile(regex);
Matcher m1 = p1.matcher(pageString);
System.out.println("m1 group count:" + m1.groupCount());
System.out.println(pageString.indexOf(regex));
assertTrue(m1.matches());

The assertion ("m1.matches") fails, even though "pageString.indexOf"
returns a value greater than -1, indicating the string is found. So
I'm missing something fundamental about regular expressions.

Any help is appreciated, - Dave
 
R

Roedy Green

The assertion ("m1.matches") fails, even though "pageString.indexOf"
returns a value greater than -1, indicating the string is found. So
I'm missing something fundamental about regular expressions.

You need to post a complete little program, not just rough fragment of
one. See http://mindprod.com/jgloss/sscce.html
The problem is nearly always in the part you did not provide. The
Murphy is in the details.

In the meantime, see http://mindprod.com/jgloss/regex.html
for working examples you can cannibalise to do what you want.
 
M

markspace

laredotornado said:
The assertion ("m1.matches") fails, even though "pageString.indexOf"
returns a value greater than -1, indicating the string is found. So
I'm missing something fundamental about regular expressions.

m1.matches() means "the regex matches the WHOLE STRING", not some
portion of it.

Use m1.region(int start, int end) to restrict the matches() method to
one part of your input string.

Or use find() (or lookingAt()) to match substrings (indexOf() uses
find(), I believe).
 
L

laredotornado

m1.matches() means "the regex matches the WHOLE STRING", not some
portion of it.

Use m1.region(int start, int end) to restrict the matches() method to
one part of your input string.

Or use find() (or lookingAt()) to match substrings (indexOf() uses
find(), I believe).

Indeed, when I changed to "find" instead of "matches", everything
worked out great. 5 stars. - Dave
 
M

markspace

laredotornado said:
Indeed, when I changed to "find" instead of "matches", everything
worked out great. 5 stars. - Dave


The Java doc is not really clear, imo, for the matches() method. The
one line documentation for that method is OK, if you already know what
matches() does. If you don't, the significance of the word "entire" in
that sentence is easy to miss.

However, there's often nice code examples on the 'net. One thing you
should always do when confronted with a problem with a class is to
Google for "java <class_name> tutorial", which will often find good
information on how to use the class in question.

For Matcher, the first link is this one:

<http://java.sun.com/docs/books/tutorial/essential/regex/matcher.html>

which has quite a lot of nice examples, including examples of how
matches(), find() and lookingAt() work and differ.
 
S

Sreenivas

This post is not related to Java, since i follow this group regularly i
am posting this.
I use ThunderBird for Usenet . I always like to see Usenet in thread
view.If want to store or preserve that thread ,is there any tools or
plugins available for this in ThunderBird?


Best,
Srinivas Reddy Thatiparthy.
 
S

Sreenivas

Sreenivas said:
This post is not related to Java, since i follow this group regularly i
am posting this.
I use ThunderBird for Usenet . I always like to see Usenet in thread
view.If want to store or preserve that thread ,is there any tools or
plugins available for this in ThunderBird?


Best,
Srinivas Reddy Thatiparthy.
My apologies for posting in somebody's thread.It's a mistake.
 
D

Daniel Pitts

laredotornado said:
Hi,

I was hoping someone could help me with a simple regular expression
dilemma. I'm using Java 1.5. I have this code:

String regex = accountid; // value is
9998698989898
System.out.println("regexp:" + regex);
Pattern p1 = Pattern.compile(regex);
Matcher m1 = p1.matcher(pageString);
System.out.println("m1 group count:" + m1.groupCount());
System.out.println(pageString.indexOf(regex));
assertTrue(m1.matches());

The assertion ("m1.matches") fails, even though "pageString.indexOf"
returns a value greater than -1, indicating the string is found. So
I'm missing something fundamental about regular expressions.

Any help is appreciated, - Dave
Matcher.matches says the whole sequence matches.

What you are possibly wanting is Matcher.find

Also, you may want to use Pattern.compile(accountid, Pattern.LITERAL);
 

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,057
Latest member
KetoBeezACVGummies

Latest Threads

Top