• 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

How to use OR in Regular Expressions?

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to write a regular expression according to the following rules:

String starts with an "A"
String continues with 4 to 6 uppercase letters "A-Z" or digits "0-9"

OR

String starts with an uppercase letter from "B-Z"
String continues with 6 digits "0-9"

The following code uses two regular expressions, one for the first case and one for the second case.

It works, but it would be better and shorter to put everything into one single regular expression. I believe it is possible because regular expressions are very powerful, but I do not know how...

Has anyone an idea how to do this?


[ April 14, 2008: Message edited by: Peter Heide ]
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use the | operator. I suggest a few hours reading the Java Tutorial about regular expressions will help no end.

BTW: Please write "boolean res = res1 || res2;" rather than using |.
 
Peter Heide
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Richie, your proposal works fine using the OR (|) is the regular expression:



Sometimes when you have a problem it looks quite complicated. When you see the solution it looks so easy.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Campbell]: BTW: Please write "boolean res = res1 || res2;" rather than using |.

I agree with this advice, but just to clarify: you should generally prefer || to | for an OR in boolean operations, e.g. in conditional expressions. This is not to be confused with the OR inside a regex, where | is what you need. Also in this particular example there is no real difference between | and ||, however in many other applications there is a difference. Generally you only want | if you're using integers, not booleans, and you want to perform bitwise operations like

If you want to operate on each bit individually like this, then | is what you want. But for boolean expressions with a single boolean result, || is clearer and also has the advantage of short-circuiting - not evaluating the second operand if it's not necessary. This is what you want probably 99% of the time, so it's good to get in the habit of writing || rather than |.

The same arguments would also apply to writing && rather than & for AND.
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Peter Heide:
Thank you Richie.

You're welcome. And thank you, Jim for the explanation of || which I ought probably to have given myself.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a thought ... using | in boolean expressions is sometimes quite essential, especially when working with the Collections Framework. Think of this:

Then the outcome of

is quite different from the outcome of

basicallly because in the first way, t.add(b) is never touched unless Set s already contains Object a.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guido: true. I rarely see code that uses | like that, and personally I would probably rewrite it (or insert a comment) in order to make it more obvious that using | wasn't just a typo. Just because people aren't used to seeing it, and someone may try to "fix" it later without realizing the true intent.
 
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
Hi Jim,

in productive code I'd surely add some comment. Plus, programming with such side effects isn't quite the style of code that is easy to read. Whole thing was just a thought ...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic