• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

To get a String matching a pattern

 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
reply
    Bookmark Topic Watch Topic
  • New Topic