Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match a specific group of digits of certain length?

I found this: Regex to match digits of specific length but it talks about Python. I am wanting to be able to get a group of random numbers of specific length. So if I have 167691#15316243 it will match 15316243. I am not sure how to implement this. right now I have new RegExp('[0-9]+', "g"); which matches a group of numbers fine, but now I realized I will have some times when I have more than one group and I only want the group of eight numbers.

like image 888
qitch Avatar asked Mar 11 '12 02:03

qitch


People also ask

How do I match a range of numbers in regex?

The regex [0-9] matches single-digit numbers 0 to 9. [1-9][0-9] matches double-digit numbers 10 to 99. Something like ^[2-9][1-6]$ matches 21 or even 96! Any help would be appreciated.

How do I match a group in regex?

Capturing groups are a way to treat multiple characters as a single unit. They are created by placing the characters to be grouped inside a set of parentheses. For example, the regular expression (dog) creates a single group containing the letters "d", "o", and "g".

How does regex Match 5 digits?

match(/(\d{5})/g);

Which regex matches one or more digits?

Occurrence Indicators (or Repetition Operators): +: one or more ( 1+ ), e.g., [0-9]+ matches one or more digits such as '123' , '000' . *: zero or more ( 0+ ), e.g., [0-9]* matches zero or more digits. It accepts all those in [0-9]+ plus the empty string.


1 Answers

You can specify the length of a matching set using {}.

For example: [0-9]{8}

Which will match any numbers from 0 to 9 with a specific length of 8 characters.

You can also specify a min/max range instead of forcing a specific legnth. So if you wanted a min of 4 and a max of 8 the example would change to: [0-9]{4,8}

like image 191
Timeout Avatar answered Oct 05 '22 11:10

Timeout