How can I extract:
using a regex, from multiple text files with Go (golang)?
You could match either 12 digits [0-9]{12}
or 10 digits and an uppercase character [0-9]{10}[A-Z]
using an or |
in a non capturing group (?:
for example:
^(?:[0-9]{12}|[0-9]{10}[A-Z])$
Or match your values between word boundaries \b
:
\b(?:[0-9]{12}|[0-9]{10}[A-Z])\b
To match one or more digits OR 10 digits followed by an uppercase character your could use this regex with word boundaries or anchored $^
:
<?php
$input = array();
$input[] = '123456789000A';
$input[] = '123456789012';
$input[] = '12345678901';
foreach($input as $i)
{
preg_match("/^([0-9]+[a-z]{1}|[0-9]{12})$/i", $i, $m);
print_r($m);
}
Output: First match, second match, third not match
Array
(
[0] => 123456789000A
[1] => 123456789000A
)
Array
(
[0] => 123456789012
[1] => 123456789012
)
Array
(
)