$string='letters=ABCD';
We want to match "letters="
and letter B
or C
. The letter in string may input as:
- B
- C
- BC
- CB
How to filter out A and D and output result as letters=BC ?
preg_match('/(letters=([BC]+))/', $string,$matches);
- This match if letter in string start letter B or C and can filter out D
- not work when letter start other letter
preg_match('/<FORMAT=[A-Z]+[BIU]+/', $string,$matches);
- The result not filter out A in result
2
Answers
If I understand the problem correctly you can replace every match of the following regular expression with an empty string.
Demo
This expression can be broken down as follows.
You could try:
See an online demo
(?:^letters=|G(?!^))
– Non-capture group to match the start of the input as literally ‘letters=’ or assert position at end of previous match but negate start of string;[BC]*
– 0+ times the letters ‘B’ or ‘C’;K
– Reset starting point of reported match;[AD-Z]+
– Match 1+ uppercase letters in range ‘AD-Z’.Replace with empty string: