Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex: Matching 4-Digits within words

Tags:

regex

perl

I have a body of text I'm looking to pull repeat sets of 4-digit numbers out from.

For Example:

The first is 1234 2) The Second is 2098 3) The Third is 3213

Now I know i'm able to get the first set of digits out by simply using:

    /\d{4}/

...returning 1234

But how do I match the second set of digits, or the third, and so on...?

edit: How do i return 2098, or 3213

like image 835
Andy 'Drew' Dodd Avatar asked Aug 24 '13 20:08

Andy 'Drew' Dodd


People also ask

What will be the code to create a text box which accepts only 4 digit numbers?

Try pattern="\d{4}" . No need to use anchors since the pattern attribute is anchored by default. Maybe type="number" maxlength="4" :-? @ÁlvaroGonzález It doesn't prevent user from entering non-digit characters.

How do you match a regular expression with digits?

To match any number from 0 to 9 we use \d in regex. It will match any single digit number from 0 to 9. \d means [0-9] or match any number from 0 to 9. Instead of writing 0123456789 the shorthand version is [0-9] where [] is used for character range.

What does .*?) Mean in regex?

(. *?) matches any character ( . ) any number of times ( * ), as few times as possible to make the regex match ( ? ). You'll get a match on any string, but you'll only capture a blank string because of the question mark.

What does (? I do in regex?

(? i) makes the regex case insensitive. (? c) makes the regex case sensitive.


1 Answers

You don't appear to have a proper answer to your question yet.

The solution is to use the /g modifier on your regex. In list context it will find all of the numbers in your string at once, like this

my $str = 'The first is 1234 2) The Second is 2098 3) The Third is 3213';

my @numbers = $str =~ /\b \d{4} \b/gx;

print "@numbers\n";

output

1234 2098 3213

Or you can iterate through them, using scalar context in a while loop, like this

while ($str =~ /\b (\d{4}) \b/gx) {
  my $number = $1;
  print $number, "\n";
}

output

1234
2098
3213

I have added the \b patterns to the regex so that it only matches whole four-digit numbers and doesn't, for example, find 1234 in 1234567. The /x modifier just allows me to add spaces so that the pattern is more intelligible.

like image 74
Borodin Avatar answered Oct 11 '22 03:10

Borodin