Sample text:
1. Medium Size +CA $33.00
2. Small Size -CA $4.00
I would like the result to be just ‘Medium Size’ or ‘Small Size’ using preg_match
So the regex should detect the ‘-‘ or ‘+’ sign and just give me the text output without the prices/currency
Tried
(S+)
(S+s+)
2
Answers
This regexp should extract the title between the first field (
1.
) and last two fields separated by spaces (+CA $33.00
):/^S+ (.+) S+ S+$/
To capture all the titles at once, use
preg_match_all()
andm
flag:Using php, you can make use of
K
to forget what is matched so far, and then get a match only for the part before the+
or-
signExplanation
^
Start of stringd+.
Match 1+ digits followed by a.
h+K
Match 1+ horizontal whitespace chars, and then forget what is matched so far usingK
[^n+-]*
Match optional char other than+
or-
or a newline[^s+-]
Match at least a single non whitespace character other than+
or-
Regex demo
If there should be a
+
or-
sign present, you can assert it:Regex demo
Or as an alternative a bit broader match starting with
S+
and capturing as few as possible characters in group 1 until the first occurrence of 1+ spaces followed by either+
or-
Regex demo