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
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.
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.
(. *?) 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.
(? i) makes the regex case insensitive. (? c) makes the regex case sensitive.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With