I need a regular expression to detect the phrase Figure 1.5:
in a given string. Also, I intend on using this expression in a PHP preg_replace()
function.
Here are some more examples:
- …are given.
Figure 2.1:
shows that… - …are given.
Figure 3:
shows that… - …are given.
Figure 1.16:
shows that… - …are given.
Figure 0.4
shows that… - …are given.
figure 5.1:
shows that…
With my limited Regex knowledge, I was able to create this:
/wFigure d*.?d*/g
But that doesn’t even begin to handle all of the permutations that could occur.
I would appreciate any suggestions that you might have.
Advertisement
Answer
There are several points here:
- You are using
w
at the start, perhaps, as a word boundary. In fact,w
matches a letter, digit or_
and actually requires this char to be at the exact location. However, there is no word char beforeFigure
, so you need to either removew
or replace withb
. preg_replace
replaces all non-overlapping occurrences by default, you do not need theg
modifierd*.?d*
is fine here, but since you want to match any digits followed with zero or more occurrences of.
and digits you can use a more specific pattern liked+(?:.d+)*
.
You can use
preg_replace('/Figure d+(?:.d+)*/', '', $string)
See the regex demo.
Details:
Figure
– a strings+
to match any one or more whitespaces, and consider addingu
flag after last/
if you need to find all Unicode whitespaces)d+
– one or more digits(?:.d+)*
– zero or more occurrences of.
and one or more digits.