Reputation: 2659
I have a variable that can contain a few variations, it also contains a number which can be any number.
The variations:
($stuksprijs+9.075);
($stuksprijs-9.075);
($stuksprijs*9.075);
($m2+9.075);
($m2-9.075);
($m2*9.075);
($o+9.075);
($o-9.075);
($o*9.075);
These are the only variations except for the numbers in it, they can change. And I need that number.
So there can be:
($m2+5);
or
($o+8.25);
or
($stuksprijs*3);
How can I get the number from those variations? How can I get the 9.075 or 5 or 8.25 or 3 from my above examples with regular expression?
I am trying to fix this with PHP, my variable that contains the string is: $explodeberekening[1]
I read multiple regex tutorials and got it to work for a single string that never changes, but how can I write a regex to get the number from above variations?
Upvotes: 0
Views: 67
Reputation: 75840
As per my comment, which seems to have worked, you can try:
^\(\$(?:stuksprijs|m2|o)[+*-](\d+(?:\.\d+)?)\);$
The number is captured in the 1st capture group. See the online demo.
A quick breakdown:
^
- Start string anchor.\(\$
- Literally match "($".(?:
- Open a non-capture group to list alternation:
stuksprijs|m2|o
- Match one of these literal alternatives.)
- Close non-capture group.[+*-]
- Match one of the symbols from the character-class.(
- Open 1st capture group:
\d+
- 1+ digits.(?:\.\d+)?
- Extra optional non-capture group to match a literal dot and 1+ digits.)
- Close 1st capture group.\);
- Literally match ");".$
- End string anchor.Upvotes: 3