none
Regex Expressions

    Question

  • Hi I need help with regex expression. Below is the output that i want.

    A11-222-333333-44

    and

    ABC-111-222222-22

    So far I found this expression /^[a-zA-Z][0-9]{7}$/ but its not working in my case. Any suggestions? Thank you!

    Tuesday, October 30, 2018 7:38 PM

Answers

  • ^[A-Z]([0-9]{2}|[A-Z]{2})-[0-9]{3}-[0-9]{6}-[0-9]{2}$

    will give you a capital letter followed by either 2 capital letters or 2 digits, then hyphen and 3 digits, hyphen and 6 digits and hyphen and 2 digits.


    Kathleen Wilber
    BrightWillow - Asp.Net Applications


    • Edited by KathyW2 Thursday, November 1, 2018 3:21 AM
    • Marked as answer by Chintan_K Thursday, November 1, 2018 5:12 PM
    Thursday, November 1, 2018 3:20 AM

All replies

  • You need to be more clear about what exactly you want. Are you describing this:

    First character must be a letter

    Second two characters either a letter or a number

    Fourth character a hyphen

    Next three characters numbers

    Next character a hyphen

    Next  six characters numbers

    Next character a hyphen

    Last two characters numbers


    ClarkNK AKA HomePage Doctor
    Database Tutorials Using ASP.NET Controls
    My recommended web hosting company: RDOServers

    Wednesday, October 31, 2018 12:26 PM
  • A11-222-333333-44      For this output I want first capital letter then next 2 should be numbers then hyphen then 3 numbers hyphen 6 numbers like that.

    ABC-111-222222-22 For this output I want first three capital letters then hyphen, 3 numbers then - 6 numbers like that.

    Wednesday, October 31, 2018 12:31 PM
  • ^[A-Z]([0-9]{2}|[A-Z]{2})-[0-9]{3}-[0-9]{6}-[0-9]{2}$

    will give you a capital letter followed by either 2 capital letters or 2 digits, then hyphen and 3 digits, hyphen and 6 digits and hyphen and 2 digits.


    Kathleen Wilber
    BrightWillow - Asp.Net Applications


    • Edited by KathyW2 Thursday, November 1, 2018 3:21 AM
    • Marked as answer by Chintan_K Thursday, November 1, 2018 5:12 PM
    Thursday, November 1, 2018 3:20 AM
  • Yeah - that's what I was gonna say -- except I'm not sure about the need for the caret at the beginning and the $ sign at the end -- ? 

    ClarkNK AKA HomePage Doctor
    Database Tutorials Using ASP.NET Controls
    My recommended web hosting company: RDOServers


    • Edited by ClarkNK Thursday, November 1, 2018 1:46 PM
    Thursday, November 1, 2018 3:49 AM
  • caret at the beginning and $ at the end means it contains the entire text. Nothing before it or after it.  If this is input into a textbox, for example, it won't pass through extra stuff typed in.

    Kathleen Wilber
    BrightWillow - Asp.Net Applications


    • Edited by KathyW2 Thursday, November 1, 2018 5:09 PM
    Thursday, November 1, 2018 5:08 PM
  • Thanks. It worked.
    Thursday, November 1, 2018 5:12 PM
  • Thanks for the explanation. But when I try it with or without the caret and dollar sign, it makes no difference.  Either way, the textbox will not accept anything more than just what the regex expression allows -- 

    Am I missing something?


    ClarkNK AKA HomePage Doctor
    Database Tutorials Using ASP.NET Controls
    My recommended web hosting company: RDOServers

    Thursday, November 1, 2018 6:21 PM
  • It depends on how you are using it. If you put it into a RegularExpresssionValidator, that control assumes the whole text must pass, with or without the ^ and $.

    If you use it in a different context - writing your own validation, for example - without the ^ and $ if the contents of a textbox is "aABC-123-123456-12b" regex will find a match to the above-mentioned expression - it will match a substring - while with the ^ and $, it won't find a match.*  If you checked for a match (Regex.Match), then took the whole content of the textbox, you could get more than you wanted.  Using the ^ and $ assures the textbox's entire contents must match.

    (*See http://regexstorm.net/tester to play with this, if you like.)


    Kathleen Wilber
    BrightWillow - Asp.Net Applications

    Thursday, November 1, 2018 7:52 PM