Embarrassing regex question

A

Aaron Fude

Hi,

I try the following and it produces no matches:

Pattern pattern = Pattern.compile("ab*s");
Matcher matcher = pattern.matcher("abacus");

while (matcher.find()) {
System.out.println(matcher.start() + " " + matcher.end());
}


I must not understand regular expressions at all. What am I missing?

Thanks!
 
E

Eric Sosman

I try the following and it produces no matches:

Pattern pattern = Pattern.compile("ab*s");
Matcher matcher = pattern.matcher("abacus");

while (matcher.find()) {
System.out.println(matcher.start() + " " + matcher.end());
}

I must not understand regular expressions at all. What am I missing?

What Stefan Ram said, to which I might add that you probably
want "ab.*s" as your pattern.
 
J

Joshua Cranmer

Hi,

I try the following and it produces no matches:

Pattern pattern = Pattern.compile("ab*s");
Matcher matcher = pattern.matcher("abacus");

while (matcher.find()) {
System.out.println(matcher.start() + " " + matcher.end());
}


I must not understand regular expressions at all. What am I missing?

What you are probably attempting is a form often known as "globbing" as
opposed to the standard regex or Perl-compatible regex.
 
D

Daniel Pitts

Hi,

I try the following and it produces no matches:

Pattern pattern = Pattern.compile("ab*s");
Matcher matcher = pattern.matcher("abacus");

while (matcher.find()) {
System.out.println(matcher.start() + " " + matcher.end());
}


I must not understand regular expressions at all. What am I missing?

Thanks!
In regex, "*" indicates 0 or more repetitions of the previous atom. In
other words, your patter will match one 'a', followed by any number of
'b', followed by an 's'

abacus does not match that description.

You can use the '.' character, which matches any. so "ab.*s" (as
someone else has pointed out), WILL match abacus.
 

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

complex regex 1
complex regex 1
java.util.regex question 3
RegExp Group Headache 1
regex question 4
Optimizing HTML links parser 10
Java Regex Problem 7
Trouble with regular expressions 8

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top