Skip to content
Advertisement

Regex to select a certain word followed by a integer or decimal?

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 before Figure, so you need to either remove w or replace with b.
  • preg_replace replaces all non-overlapping occurrences by default, you do not need the g modifier
  • d*.?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 like d+(?:.d+)*.

You can use

preg_replace('/Figure d+(?:.d+)*/', '', $string)

See the regex demo.

Details:

  • Figure – a string
  • – a space (replace with s+ to match any one or more whitespaces, and consider adding u 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.
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement