Amandeep Singh Amandeep Singh - 4 months ago 9
PHP Question

How to match digits between strings not between numbers

Is it possible to write a regular expression that matches with digits that does not have integer on left or right side? If we have these strings:

a20c
20c
.20c
a20-
120


It should match the four first, but not the last one.

Answer

This regex patterns will match text that has the 20 somewhere in the middle and of which no other number is touching the 20. This could occur anywhere in the text, therefore matching DM11 20-B but not DM1120-B.

[^0-9]20[^0-9]

Or a little more condensed:

\D20\D
Comments