aspose file tools*
The moose likes Beginning Java and the fly likes It is about regex Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "It is about regex" Watch "It is about regex" New topic
Author

It is about regex

Nakul P. Patel
Greenhorn

Joined: May 31, 2011
Posts: 25
I have String : "extend cup end table"

case 1:If i use patter "e.+d" it will find() match: extend cup end

case 2:If i use patter "e.+?d" it will find() match: extend end

Can someone please let me know what is difference between ".+" and ".+?"

I know that + is one or more and ? is one or zero. But i am not able to understand how does it work.

Thanks. Nakul
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36499
    
  16
? is one or zero, yes, but not after +
It is a quantifier, and you can read about them here.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

Nakuli Patidar wrote:I have String : "extend cup end table"

case 1:If i use patter "e.+d" it will find() match: extend cup end

case 2:If i use patter "e.+?d" it will find() match: extend end


Also, you should also mention that your find() method calls are in a loop -- and in the second case, it is not a single result. The way you worded your question, you can confuse some beginners to regular expressions.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36499
    
  16
It confused me, too; I expected to see “extend” there.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: It is about regex
 
Similar Threads
Indian Team is World Champion
url matching doubt
It is about regex
regarding facade pattern
when extend a class