The moose likes Java in General and the fly likes Regex Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Regex" Watch "Regex" New topic
Author

Regex

Krishan Chauhan
Ranch Hand

Joined: Mar 12, 2008
Posts: 32
Hi All

Could any one let me know how quantifier actually works?

When i tried to search using pattern "a.c" in "abc 34 a cac", i got the expected output i.e
0 abc
7 a c
So, Metacharacter . is working here.

but if i use some quantifiers like "a.c*" or "a.c?", then output is coming something like this
0 abc
7 a c
10 ac

Here even ac is coming in output, what happened to metacharacter here?
Could anyone explain how metacharacter and quantifiers are working here?


Thanks
Krishan
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 32767
The dot matches the "c". Both "?" and "*" mean that the character they follow can be missing, so they match no characters.


Android appsImageJ pluginsJava web charts
Krishan Chauhan
Ranch Hand

Joined: Mar 12, 2008
Posts: 32
Hi Ulf

do you mean to say that metacharacters do not matter when using quantifiers?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18652
It's not that the metacharacter doesn't matter at all. But if the quantifier can be zero, then the pattern can match zero occurrences of the metacharacter. So there are at least some possible matches where the metacharacter does not occur at all.

Try using + instead of * - you will see that + means at least one occurrence of the preceding character. So it will not give you this behavior of "ignoring" that character.
[ May 13, 2008: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Krishan Chauhan
Ranch Hand

Joined: Mar 12, 2008
Posts: 32
Hi Jim

Thanks for your reply.

When you use quantifiers, does it account for all the characters preceding it or the immediate one? Do we have any option regarding this?

If i use seach pattern as a.*c in place of a.c*, it works like a greedy quantifier. Could any one tell me what's the difference between above two pattern?


Thanks
Krishan
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 26710
Have you been through the Java Tutorials? It explains quantifiers there.
arulk pillai
Author
Ranch Hand

Joined: May 31, 2007
Posts: 2885
a.* --> means a followed by any character zero or more times
a.c* --> means a followed by any one character followed by character c zero or more times

also look at +, ?


check here --> http://java.sun.com/docs/books/tutorial/essential/regex/quant.html


Java Interview Questions and Answers Blog | Amazon.com profile | Java Interview Books
Krishan Chauhan
Ranch Hand

Joined: Mar 12, 2008
Posts: 32
Hi

It solved most of my problem but i still have one doubt:

If a.c* means a followed by any one character followed by character c zero or more times why does it ignores 34 in "abc 34 a cac" ?
Please help.




Thanks
Krishan
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 32767
I don't understand your question - there's no "a" in "34", so why do you think it should match it?
 
 
subject: Regex
 
Threads others viewed
double anamoly
how to draw dynamic menu
Question about string pattern matching
what is hash tree
PatternSyntaxException with * in regex
developer file tools