Trying to find groups of letters with regular expression

I need to find 1 or more specific groups of characters enclosed in parentheses. If more than one group is present, it will be separated by a hyphen.

Example:

(us) (jp) (jp-us) (jp-us-eu) 

I figured out how to find a group if the line contains only one group:

 /\(us\)|\(jp\)/ 

However, I get confused when it comes to finding more than one, separated by hypnosis and in any particular order: (us-jp) OR (jp-us)

Any help is appreciated.

Thanks Simon

+7
php regex find search
source share
2 answers
 \((\b(?:en|jp|us|eu)-?\b)+\) 

Explanation:

 \ (// opening paren
 (// match group one
   \ b // word boundary
   (?: en | jp | us | eu) // your defined strings
   -?  // a hyphen, optional
   \ b // another word boundary
 ) + // repeat
 \) // closing paren

matches:

 (us) (jp) (jp-us) (jp-us-eu) 

does not match:

 (jp-us-eu-) (-jp-us-eu) (-jp-us-eu-) 
+13
source share

Try the following:

 /\([az]{2}(?:-[az]{2})*\)/ 

This will match any sequence of two letters in brackets that mine contains more than two letter sequences separated by hypens. So (ab) , (ab-cd) , (ab-cd-ef) , (ab-cd-ef-gh) , etc.

+2
source share

All Articles