1
votes

I want to filter a number out of a string with php preg_match that could look like this (they are versions of a software):

  • 1.6.2
  • 1.6.2#2
  • 1.7

(1.6.2#2 is 2. beta of 1.6.2)

So when there is a string like:

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, 1.6.2# sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.

Now I want a string that only contains "1.6.2#2"

I tried (\d?[.|#]?)* but it does not work

1
How does it not work? Do you get any errors? Also see: regex101.com/r/xU9hH8/1 - Rizier123
Because there are only 3 single strings and not one hole.. - Zoker
what about version numbers like 1.6beta.7devel? - Marc B
Beta is covered by the #2, so 1.6.2#2 is 2. beta of 1.6.2 - Zoker

1 Answers

2
votes

Use this regular expression

/(\d+\.\d+(\.\d+)?(#\d+)?)/