Regular expression : match either of two conditions?

22,247

Solution 1

In your regex, the two alternative branches are anchored separately:

  • (^([a-zA-Z]){2}([0-9]){6}) - 2 letters and 6 digits at the start of the string
  • | - or
  • ([0-9]*)?$ - optional zero or more digits at the end of the string

You need to adjust the boundaries of the group:

data-ng-pattern="/^([a-zA-Z]{2}[0-9]{6}|[0-9]{8})?$/"
                   ^                         ^^^^ 

See the regex demo.

Now, the pattern will match:

  • ^ - start of string
  • ( - start of the grouping:
    • [a-zA-Z]{2}[0-9]{6} - 2 letters and 6 digits
    • | - or
    • [0-9]{8} - 8 digits
  • )? - end of the grouping and ? quantifier makes it match 1 or 0 times (optional)
  • $ - end of string.

Solution 2

You can try this DEMO LINK HERE

^(([a-zA-Z]{2}|[0-9]{2})[0-9]{6})?$

It will accept:

  1. ab123456
  2. 12345678
  3. aa441236
  4. aw222222
Share:
22,247
shreyansh
Author by

shreyansh

Web Developer

Updated on September 22, 2020

Comments

  • shreyansh
    shreyansh over 3 years

    Hi I don't know much about regular expression. But I need it in form validation using angularJs.

    Below is the requirement

    The input box should accept only if either

    (1) first 2 letters alpha + 6 numeric

    or

    (2) 8 numeric

    Below are some correct Inputs :-

    (1)SH123456 (2)12345678 (3)sd456565

    I tried data-ng-pattern="/(^([a-zA-Z]){2}([0-9]){6})|([0-9]*)?$/" , Its working fine for both the above condition but still it is accepting strings like S2D3E4F5 and may be many other combination as well.

    What I am doing wrong I am not able to find it out.

    Any help is appreciable !!!

    Thanks

  • Wiktor Stribiżew
    Wiktor Stribiżew over 7 years
    It is semantically the same as mine, so it will also work. The formal difference is just 1 nested capturing group is used here while mine has no nested groups. Both have similar performance.
  • Md. Rahman
    Md. Rahman over 7 years
    6 numeric values are common for both case. that's why I used a common part for 6 numeric values. and there are many ways to write regular expression. :)
  • jensgram
    jensgram over 7 years
    Equivalent to ^([a-zA-Z0-9]{2}[0-9]{6})?$, it seems.
  • Md. Rahman
    Md. Rahman over 7 years
    No. your expression will also except a1234567. But its need First 2 letters alpha + 6 @jensgram
  • Wiktor Stribiżew
    Wiktor Stribiżew over 7 years
    @revo: If someone downvotes your answers, probably there is something wrong with them. I did not express any concern, I only asked for a "constructive critique". Plain downvotes do not add value when the answer given is detailed, working and helpful.
  • revo
    revo over 7 years
    In a normal case that's right but not times when you receive a down-vote. For sure I'll do what I said if I see the same behavior another time.
  • Wiktor Stribiżew
    Wiktor Stribiżew over 7 years
    @revo Please do it now.
  • revo
    revo over 7 years
    I'll do it certainly but in a more appropriate time when I'm sure my complaint is going to take into account.