• Post Reply Bookmark Topic Watch Topic
  • New Topic

To get a String matching a pattern  RSS feed

 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!