| 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
|
|
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
|
|
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
|
|
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!
|
 |
 |
|
|
subject: To get a String matching a pattern
|
|
|