File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes To get a String matching a pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "To get a String matching a pattern" Watch "To get a String matching a pattern" New topic
Author

To get a String matching a pattern

Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300
Hi All,

It seems to be simple to do but i am not able to get it.

I want to know whether a string contains letter or not.
I tried to use string.matches("\\w+") but it does not help me anyway.

Kindly help me in doing it.
Thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16692
    
  19

The matches method matches the whole string -- it doesn't do partial matches like "string contains xxx". To do that, either use the find() method, or modify the regex to match around what you are looking for like... string.matches(".*\\w+.*").

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
Originally posted by Henry Wong:
The matches method matches the whole string -- it doesn't do partial matches like "string contains xxx". To do that, either use the find() method, or modify the regex to match around what you are looking for like... string.matches(".*\\w+.*").

Henry


You actually need string.matches(".*?\\w+.*"), the other won't work. In general, it's a good idea to make leading "match-all" parts reluctant, for otherwise they might match some part of your input you're aiming at with some later part ...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16692
    
  19

Originally posted by Guido Sautter:

You actually need string.matches(".*?\\w+.*"), the other won't work. In general, it's a good idea to make leading "match-all" parts reluctant, for otherwise they might match some part of your input you're aiming at with some later part ...


Okay, I'll ask. Why don't you think it will work? Can you give one case?

This is a call to the matches() method, not the find() method -- there is no "some later part". Everything has to be matched in one shot.

Henry
[ September 10, 2008: Message edited by: Henry Wong ]
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
I'll have to test this particular one, but have generally pretty bad experiance with leading '.*' parts in regular expressions ... therefore, I've found it better to make them reluctant ...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16692
    
  19

Originally posted by Guido Sautter:
I'll have to test this particular one, but have generally pretty bad experiance with leading '.*' parts in regular expressions ... therefore, I've found it better to make them reluctant ...


The two problems that I can think of is... (1) If you have a possessive qualifier later in the regex, then an eariler greedy qualifier may not work. (2) If you are using the find() method, then a greedy qualifier may take too much in eariler calls, and affect future calls.

Neither case is true here. It should work.

Henry
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
Originally posted by Guido Sautter:
I'll have to test this particular one, but have generally pretty bad experiance with leading '.*' parts in regular expressions ... therefore, I've found it better to make them reluctant ...


As Henry already mentioned: it doesn't make a difference in this case. But being cautious of those greedy .* and .+ critters doesn't hurt when constructing regexes!
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: To get a String matching a pattern
 
Similar Threads
How can I get Unicode String of a String?
new page on submission
how to get a prompt
struts request values
get the length of the string