-1

I need an regular expression for RR-1000 i.e First two digits must be alphabets and it must be followed by an '-'(hyphen) then there must be four numbers .The total length should not be greater than 7 digits.

user3383301
  • 1,891
  • 3
  • 21
  • 49

1 Answers1

1

Try:

([A-Za-z]{2})[-](\d{4})

You can see it as follows:

Regular expression visualization

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
  • 1
    You don't even need the parentheses or brackets; `[A-z]{2}-\d{4}` should work fine. – Andrew May 12 '14 at 16:59
  • @AndrewArnold A few languages define `-` differently. Since I primarily work in Lua, the `-` means a lazy match. And hence, my practice of enclosing it as `[-]` – hjpotter92 May 12 '14 at 17:01
  • True, it depends on language. My answer is valid in Javascript, at the very least. – Andrew May 12 '14 at 17:01
  • I'm curious as to what language defines `-` differently. – Jerry May 12 '14 at 17:03
  • 1
    @Jerry http://www.lua.org/pil/20.2.html Search the following on the page: "Patterns in Lua offer four modifiers" – hjpotter92 May 12 '14 at 17:04
  • I wouldn't call it regex, but more like a Lua expression if you ask me xP And if you did write a Lua pattern, you'd have to use `%a%a%-%d%d%d%d` instead of what you have used above. – Jerry May 12 '14 at 17:09
  • @Jerry of course; but I've been writing lua patterns for so long that `[-]` is just muscle memory :P – hjpotter92 May 12 '14 at 17:15
  • 2
    Lol, ok. Though your regex isn't entirely correct. See [this](http://stackoverflow.com/a/18661919/1578604). – Jerry May 12 '14 at 17:21