简体   繁体   中英

How can I get all occurrences of this pattern with the regex of PHP?

How can I get, into an array, all occurrences of this pattern 4321[5-9][7-9]{6} but excluding, for example, the occurrences where there is a digit immediately before the value, or immediately after it?

For instance, 43217999999 should be valid but 1 43217999999 (note the number 1 at the beginning) should not be valid.

As the first example, 43217999999 1 shouldn't be valid because of the 1 that it has in the end.

The added difficulty, at least for me, is that I have to parse this in whatever position I can find it inside a string.

The string looks like this, literally:

43217999997 / 5 43217999999 // 43217999998 _ 43217999999a43216999999-43216999999 arandomword 43215999999 7

As you would be able to note, it has no standard way of separating the values (I marked in bold the values that would make it invalid, so I shouldn't match those)

My idea right now is something like this:

(\D+|^)(4321[5-9][7-9]{6})(\D+|$)

(\\D+|^) meaning that I expect in that position the start of the string or at least one non-digit and (\\D+|$) meaning that I expect there the end of the string or at least one non-digit.

That obviously doesn't do what I picture in my head.

I also tried do it in two steps, first:

preg_match_all("/\D+4321[5-9][7-9]{6}\D+|4321[5-9][7-9]{6}\D+|4321[5-9][7-9]{6}$/", $input, $outputArray);

and then:

for($cont = 0; $cont < count($outputArray); $cont++) {
   preg_match("/4321[5-9][7-9]{6}/", $outputArray[0][$cont], $outputArray2[]);
}

so I can print

echo "<pre>" . print_r($outputArray2, true) . "</pre>";

but that doesn't let me exclude the ones that have a number before the start of the value ( 5 432157999999 for example), and then, I am not making any progress with my idea.

Thanks in advance for any help.

If you literally want to check if there is no digit before or after the match you can use negative look ahead and look behind.

  • (?![0-9]) at the end means: "is not followed by 0-9"
  • (?<![0-9]) at the start means: "is not preceded by 0-9"

See this example https://regex101.com/r/6xbmJk/1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM