Reputation: 4560
everyone. I want to ask help writing the statement of regex in PHP
// This will match everything between "(" and ")"
'#('.preg_quote('(').')(.*)('.preg_quote(')').')#si'
//But I want to add one more condition
//-> Only match before "(" contains at least 2 Capital Letters continued.
For example,
// (bbbbb) <- No
// aa(bbbbb) <- No
// A(bbbbb) <-No
// AA(bbbbb) <- Yes
// AAA(bbbbb) <- Yes
How can I do it?
Thank you very much!!
Upvotes: 2
Views: 98
Reputation: 914
$re = "/^[A-Z]{2,}\([a-z]{5}\)$/";
I also added ^
and $
. You can remove those operators if you're wanting to search a match within a string, but if you want to strictly match the whole string passed to preg_match
, you may want to keep them.
Upvotes: 3
Reputation: 71384
You can make regex like this:
$regex = '#[A-Z]{2,}\((.*)\)#';
Note I have removed case insensitive pattern modifier i
as you DO want case sensitivity. I have also removed s
pattern modifier as my guess is you don't want this comparison across multiple lines.
You might consider ungreedy pattern modifier U
if you expect that there might be multiple un-nested sets of parenthesis on a line and you want to capture them separately (i.e. AA(bbbbb) CC(ddddd)
).
Also notice I got rid of all that preg_quote()
non-sense as it makes your pattern harder to read. That function is best saved for cases where you might have variable string data you are inserting into the pattern which you need to escape. That is not the case here.
Upvotes: 7