regex question

A

Alex

Why results are different? Why second one can't find pattern?
public static void main(String[] args) {
Pattern pattern = Pattern.compile ("a*\\.process,RUN*");
Matcher matcher = pattern.matcher
("apps_111.process,RUNNING");
System.out.println(matcher.find());// true
Pattern pattern1 = Pattern.compile
("apps*\\.process,RUN*");
Matcher matcher1 =
pattern1.matcher("apps_111.process,RUNNING");
System.out.println(matcher1.find());// false
}
Alex.
 
W

Wibble

Alex said:
Why results are different? Why second one can't find pattern?
public static void main(String[] args) {
Pattern pattern = Pattern.compile ("a*\\.process,RUN*");
Matcher matcher = pattern.matcher
("apps_111.process,RUNNING");
System.out.println(matcher.find());// true
Pattern pattern1 = Pattern.compile
("apps*\\.process,RUN*");
Matcher matcher1 =
pattern1.matcher("apps_111.process,RUNNING");
System.out.println(matcher1.find());// false
}
Alex.
You mean to use ".*" not "*".

"a*_" matches "aaaaaaaa_"
"a.*_" matches "apps_"
 
E

Eric K Idema

Alex said:
Why results are different? Why second one can't find pattern?
public static void main(String[] args) {
Pattern pattern = Pattern.compile ("a*\\.process,RUN*");
Matcher matcher = pattern.matcher
("apps_111.process,RUNNING");
System.out.println(matcher.find());// true
Pattern pattern1 = Pattern.compile
("apps*\\.process,RUN*");
Matcher matcher1 =
pattern1.matcher("apps_111.process,RUNNING");
System.out.println(matcher1.find());// false
}

* matches the preceding character zero or more times. So, a snippit like:

RUN*

Would match RU followed by zero or more N's ("RU", "RUN", "RUNNNNN", etc).

To match any character zero or more times use .*

Eric
 
R

Roedy Green

Why results are different? Why second one can't find pattern?
public static void main(String[] args) {
Pattern pattern = Pattern.compile ("a*\\.process,RUN*");
Matcher matcher = pattern.matcher
("apps_111.process,RUNNING");
System.out.println(matcher.find());// true
Pattern pattern1 = Pattern.compile
("apps*\\.process,RUN*");
Matcher matcher1 =
pattern1.matcher("apps_111.process,RUNNING");
System.out.println(matcher1.find());// false
}
Alex.

* in regex has a complexly different meaning from * in wildcards. See
http://mindprod.com/jgloss/regex.html

It does not mean (allow any string of chars here).
 
J

John C. Bollinger

Wibble said:
Alex said:
Why results are different? Why second one can't find pattern?
public static void main(String[] args) {
Pattern pattern = Pattern.compile ("a*\\.process,RUN*");
Matcher matcher = pattern.matcher
("apps_111.process,RUNNING");
System.out.println(matcher.find());// true
Pattern pattern1 = Pattern.compile
("apps*\\.process,RUN*");
Matcher matcher1 =
pattern1.matcher("apps_111.process,RUNNING");
System.out.println(matcher1.find());// false
}
Alex.
You mean to use ".*" not "*".

"a*_" matches "aaaaaaaa_"
"a.*_" matches "apps_"

And specifically to the OP's question, "a*_" also matches "_".
Furthermore, Matcher.find() returns true if the pattern matches any
portion of the target string. Putting that together, the part of the
target string that the first pattern matches is ".process,RUNN". There
is no part of the target string that is matched by the second pattern.

Alex: do read up on regular expressions. An excellent resource would be
the book "Programming in Perl" (or alternatively, the Perl manual) which
covers the topic in considerable detail. Java's regular expressions are
pretty closely compatible with Perl's. Do also read the docs on the
Matcher class, and make sure you understand the differences between its
matches(), find(), and lookingAt() methods. (And if you do read up on
Perl regexes, don't trip over the fact that Perl jargon uses the term
"matches" for the behavior exhibited by Matcher.find(), not
Matcher.matches().)
 

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

Similar Threads

Embarrassing regex question 5
regex problem 9
RegEx problem 5
Certificate validation 1
Hostname verifier 0
regex 12
regexp(ing) Backus-Naurish expressions ... 23
Grouping Regex question 2

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,272
Latest member
MaricruzDu

Latest Threads

Top