Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correct my regex to not match particular expressions starting with a character

Tags:

regex

php

In PHP I am trying to create a regex without success, which should match expressions consisting of numbers and a single 'end'character, only of the numbers are not preceeded by an F. Here is an exmaple string to match

$test = "123A456C789XF765S333333AF444G";

and the following regex

preg_match_all("/(?<!F)(\d+)([ASTCGX])+/", $test, $matches)

finds the following expressions:

123A
456C
789X
65S
33333A
44G

while I expect only the following matches:

123A
456C
789X
333333A

I do not want to have the numbers \d+ split up to match the regex. An expression like F\d+[ACXSG]+ should not be matched! How to achieve this?

like image 634
Alex Avatar asked Nov 18 '25 00:11

Alex


1 Answers

I suggest you to use this:

preg_match_all("/(?<![\dF])(\d++)([ASTCGX])/", $test, $matches);

explanation:

(?<![\dF])       # not preceded by F or a digit (numbers can't be trunked)
\d++             # one or more digit (with a possessive quantifier, optional but better)
[ASTCGX]         # one of these characters 
like image 196
Casimir et Hippolyte Avatar answered Nov 19 '25 14:11

Casimir et Hippolyte