Take the 2-minute tour ×

Right now I have two regular expressions that does this:

Regex-1 = "(\\{(\\w+)\\})";
Regex-2 = "(\\{([^\\|]*)\\|([^\\|]*)\\|([^\\|]*)\\})"; 

I just want to be able to combine the two Regex into one, so that which ever Regex searches first will be the match.

Conceptually i'm thinking regex-1 || regex-2. Is that possible to combine them?


share|improve this question
Can you provide a couple examples of what it is you are trying to match? –  JDB Jul 4 '12 at 1:48
rubular.com/r/YxzHacVbtd Regex-1 only matches if there's only 1 value in curly braces. Regex-2 matches for 3 values. –  unwise guy Jul 4 '12 at 1:51
add comment

1 Answer

up vote 1 down vote accepted

You mean like this?

Regex-1_2 = "(\\{(\\w+)\\})|(\\{([^\\|]*)\\|([^\\|]*)\\|([^\\|]*)\\})";


So, something like this?



Your last comment helps. So, work from beginning to end. You know you want to match an opening curly bracket, so that part is easy:


Now, after the first curly bracket, there are two options. Either there will be one or more alphanumeric (or underscore) characters, or there will be three groups of zero or more characters separated by pipes. The first two groups must be non-pipe characters, while the last group must be non-curly bracket characters (since a curly bracket will close the expression). That can be expressed using the alternation construct:


Finally, the expression ends with a curly bracket:


This works with the example you provided. If your rules are different or more specific, you will need to say so.

share|improve this answer
close, but not quite.. that works if Regex-1 matches first, but if Regex-1 matches after Regex-2, then it doesn't work. –  unwise guy Jul 4 '12 at 1:38
rubular.com/r/YxzHacVbtd –  unwise guy Jul 4 '12 at 1:42
@unwiseguy - updated my answer. –  JDB Jul 4 '12 at 3:47
that was awesome. –  unwise guy Jul 4 '12 at 5:04
add comment

Your Answer


