Xaisoft
Xaisoft

Reputation: 46651

Partial search with regex?

If I am using a RegEx as a mask for a TextBox and the mask should allow a format of 000-XXXXXX, meaning for example that it allows 3 letters, a dash, then 6 numbers, how can I allow the user to only have to type in the first 3 characters of the mask to use in searching and not have what they typed in be invalid because it does not satisfy a the complete RegEx?

Upvotes: 2

Views: 106

Answers (1)

Tim Pietzcker
Tim Pietzcker

Reputation: 336438

You can make parts of the regex optional:

^\d{3}(?:-\d{0,6})?$

Explanation:

^        # Start of string
\d{3}    # Match 3 digits
(?:      # Try to match... 
 -       #  a dash
 \d{0,6} #  followed by up to 6 digits
)?       # but make that part of the match optional
$        # End of string

Upvotes: 2

Related Questions