This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes How to use OR in Regular Expressions? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to use OR in Regular Expressions?" Watch "How to use OR in Regular Expressions?" New topic
Author

How to use OR in Regular Expressions?

Peter Heide
Ranch Hand

Joined: Nov 04, 2006
Posts: 31
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 ]

SCJA
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
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

Joined: Nov 04, 2006
Posts: 31
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.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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.


"I'm not back." - Bill Harding, Twister
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
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.
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
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
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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

Joined: Dec 22, 2004
Posts: 142
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 ...
 
wood burning stoves
 
subject: How to use OR in Regular Expressions?
 
Similar Threads
Problem with Regex
Regular expression - Pattern and matcher
Regex Woes
Two Regular Expressions in one
Regex clarification