Sanju
Sanju

Reputation: 903

Want a regular expression to validate a number format

I have a specific number format as: 1-2345678908765 where first digit is separated by '-' and thirteen number following it. I tried using:

[]?[0-9]\-^[0-9]{1,13}$

but didn't work.

Thanks in Advance

Sanjay Suman

Upvotes: 0

Views: 86

Answers (5)

help-info.de
help-info.de

Reputation: 7260

Working on this please try for grouping and may be this is usefully for your needs in future e.g. find and replace. If you want to find all lines beginning with "1":

(^1{1})-([0-9]{13})

To exchange the first and second value you pnly need a "\2-\1" in the "Replace" textbox of NotePad++ for example.

enter image description here

BTW - please check https://www.regex101.com/ for your needs.

Upvotes: 0

hwnd
hwnd

Reputation: 70732

It seems your regular expression syntax is malformed, the first issue is that []?[0-9] if using (PCRE) forms a character class which matches any one character from the set of characters.

Otherwise, []? does a zero-width match because you have an empty optional class.

You also have the beginning of string ^ anchor placed after the hyphen in which the following numbers do not assert at that given position in the string.

You can use the following regex instead:

^[0-9]-[0-9]{13}$

Explanation:

^            # the beginning of the string
 [0-9]       #   any character of: '0' to '9'
 -           #   '-'
 [0-9]{13}   #   any character of: '0' to '9' (13 times)
$            # before an optional \n, and the end of the string

Live Demo

Upvotes: 3

Sanju
Sanju

Reputation: 903

I fixed it finally.

[]?[0-9]-[0-9]{13}

Upvotes: 0

Össi
Össi

Reputation: 41

Try /^[0-9][\-][0-9]{13}$/gm

You can test out regular expressions at regexr.com

Upvotes: 0

mortaZa
mortaZa

Reputation: 115

"/^[0-9][-][0-9]{13}$/"

It will match a single number, followed by 1 hypen, followed by 13 numbers.

Upvotes: 0

Related Questions