• 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

regex question

 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
New to java regex. If I want to check if a String contains several special characters "a", "b" or "c" ( [abc] ), how should I do that ? a snippet ? I just want to see what method I should use...

Thanks.
 
Marshal
Posts: 79401
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know precious little about regex myself, but there is this section in the Java tutorial which should be helpful to you.
 
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 ben oliver:
New to java regex. If I want to check if a String contains several special characters "a", "b" or "c" ( [abc] ), how should I do that ? a snippet ? I just want to see what method I should use...

Thanks.



Java regular expressions is a pretty big topic -- both regular expressions itself, and the options with the Java API. It is certainly not something that you can get a snippet, and use it as a skeleton that will work for everything. In many cases, there are many ways to do the same thing, and you'll need to understand your options to find the best choice.

Anyway... one of the options is that the regex engine is integrated to the String class, so you can check to string itself to see if it contains those characters.



Henry
 
ben oliver
Ranch Hand
Posts: 375
  • 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:


Henry



since I just care if the String contains "a", "b", or "c" for any times, can I do --

Pattern p = Pattern.compile("[abc]+");
Matcher m = p.matcher(text);
m.find(); // returns true if it finds any of the 3 characters for any times
 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe my question was too silly so nobody echoed.. Anyway, I would like to ask the same question again --

If I want to find out if a String text contains "double quote", "a", "b", or "c" for any times, can I do --


Pattern p = Pattern.compile("[\"abc]+");
Matcher m = p.matcher(text);
m.find(); // returns true if it finds any of the 3 characters for one or more occurance
 
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 ben oliver:

since I just care if the String contains "a", "b", or "c" for any times, can I do --

Pattern p = Pattern.compile("[abc]+");
Matcher m = p.matcher(text);
m.find(); // returns true if it finds any of the 3 characters for any times



Yes... find() will also work -- if you are looking for any match.

Henry
 
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 ben oliver:
Maybe my question was too silly so nobody echoed.. Anyway, I would like to ask the same question again --

If I want to find out if a String text contains "double quote", "a", "b", or "c" for any times, can I do --


Pattern p = Pattern.compile("[\"abc]+");
Matcher m = p.matcher(text);
m.find(); // returns true if it finds any of the 3 characters for one or more occurance



I am assuming that you have tried it, and it works? The best way to learn regex is to just play with it.

Henry
 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried and it worked. Now I have a question --

what's the difference between "[abc]" and "[abc]+" if I just want to do "find" (search) ??

"[abc]" means any of the characters in the [] (then by default it is one or more, if it is zero condition fails..)
"[abc]+" means one or more characters from the [].

So they have no difference in terms of using "find" to search. What do you think ??
 
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 ben oliver:
what's the difference between "[abc]" and "[abc]+" if I just want to do "find" (search) ??

"[abc]" means any of the characters in the [] (then by default it is one or more, if it is zero condition fails..)
"[abc]+" means one or more characters from the [].

So they have no difference in terms of using "find" to search. What do you think ??



In the first case, it will match exactly one. In the second case, it will match one or more.

If you only call the find() method once, to see if there is a match, then it won't make a difference. If you call it multiple times, or actually use the results, the number and the results may be different.

Henry
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
regex are very precious and helped me a lot since the day a learned them, their power are often underestimated to the people who don't know them.

a little help from my side though it does not directly answer your questions. i found regex in java code always very clumsy. if it's possible (just a little script or similar), have a look at regex apis from great scripting languages like ruby or python!

and further more it is much more fun to expermiment with regex-patterns in scripting languages: no compilation, shorter code etc..
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic