0

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?

Thanks.

unwise guy
  • 1,048
  • 8
  • 18
  • 27
  • Can you provide a couple examples of what it is you are trying to match? – JDB Jul 04 '12 at 01: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 04 '12 at 01:51

1 Answers1

1

You mean like this?

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

EDIT:

So, something like this?

{(((\w+)|({\w+}))\|?)*}

EDIT 2:

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:

{(\w+|[^|]*\|[^|]*\|[^}]*)

Finally, the expression ends with a curly bracket:

{(\w+|[^|]*\|[^|]*\|[^}]*)}

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

JDB
  • 25,172
  • 5
  • 72
  • 123
  • 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 04 '12 at 01:38