0

I have the following regular expression and I want the matching alternative of the first group to be the matching alternative of the second group.

(?i)^([a-z]+|\d+)-([a-z]+|\d+)$

Basically what I want is if \[a-z\] matches in the first group I want only that pattern to match the second group and if \\d matches in the first group I want only that pattern to match in the second group.

I tried with an expanded regular expression that had[a-z]+)-([a-z]+)|(\d+)-(\d+) but that gave me 4 groups either 1,2 or 3,4 with one set populated and the other set null.

I want to make it where there is always just groups 1,2 so I don't have to test to see which groups actually match.

Given the following input:

10-15
XX-ZZ
5-A
a-1000

10-15 should match
XX-ZZ should match
5-A should not match
a-1000 should not match

Community
  • 1
  • 1

3 Answers3

1

Use a Conditional

This is your regex:

/(?i)^([a-z]+|\d+)-([a-z]+|\d+)$/

Please see the following:

/(?i)^(?:([a-z]+)|\d+)-(?(1)[a-z]+|\d+)$/

Regex Demo

Unihedron
  • 10,902
  • 13
  • 62
  • 72
  • 3
    This is what I was working on figuring out, thanks, but this isn't going to work for me, I just found out that Java doesn't support conditional references. But you get the checkmark because I didn't specify a language implementation. –  Jul 31 '14 at 16:02
0

Unless I am missing some obvious I think following regex should work for you:

(?im)^(?:([a-z]+-[a-z]+)|(\d+-\d+))$

RegEx Demo

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • this misses the point completely, see my example that I linked to in the question or the accepted answer, this is not an answer –  Jul 31 '14 at 16:08
  • May I know what point it misses. Also did you check my demo link which gives your expected output? – anubhava Jul 31 '14 at 16:12
  • Also IMO accepting a PCRE regex for question tagged for Java is not making sense as there are several easy lookbehind based regex for regex questions tagged as Javascript. But that still doesn't make them a valid answer for a Javascript regex problem. – anubhava Jul 31 '14 at 16:16
  • 1
    @anubhava I see OP revoked the [tag:java] tag while we discuss about the credibility of my answer. I agree with your standpoint that your solution actually did solve the problem (as it was before edits during the grace period anyway) and doesn't deserve its downvote, so I +1'd. Mind removing `m` from `(?im)` though? – Unihedron Jul 31 '14 at 16:24
  • **READ FOR COMPREHENSION** - this has **NEVER** answered the question regardless of tags! It doesn't do what mine does, it doesn't even get close to grouping the things on either side of the `-` into groups! How does `(10)-(15)` == `(10-15)`? It doesn't, thus this is **not an answer**! –  Jul 31 '14 at 16:30
  • @JarrodRoberson: You need to seriously read your question again. You wrote this regex worked for you: `(\[a-z\]+)-(\[a-z\]+)|(\\d+)-(\\d+)` Are you really serious about `\[` and `\]`? Since you don't have literal `[` or `]` in your input. – anubhava Jul 31 '14 at 16:35
  • the escaping didn't take effect correctly and yes it *works* but it gives 4 groups **I want only 2 groups**! What part of that is not clear? –  Jul 31 '14 at 16:38
  • Since you had to recently edit your question to correct your regex and plus you had to earlier remove original `Java` tag verifies that question didn't provide all the facts correctly. In any case my solution never had 4 captured groups like yours. – anubhava Jul 31 '14 at 16:44
  • drop it about the capture groups, this doesn't even match what I am doing **at all** I want what is on either side of the `-`; **read for comprehension** –  Jul 31 '14 at 18:42
0

Here is an answer that doesn't rely on conditional references:

(?i)^(?=\[a-z\]+-\[a-z\]+$|\d+-\d+$)(\[a-z\d\]+)-(\[a-z\d\]+)$

This is what it was used for.

Community
  • 1
  • 1