ruby - Regular expression match that excludes characters inside parenthesis -


i have following types of strings.

bill smith (usa) winthrop (fr) lord @ war (gb) kim smith 

with these strings, have following constraints: 1. caps 2. can 2 18 charters long 3. should not have white spaces or carriage returns @ end 4. country abbreviation inside parens should excluded 5. of names not have country in parens , should matched too

after applying regular expression i'd following:

bill smith (usa)  => bill smith winthrop (fr) => winthrop lord @ war (gb) = lord @ war kim smith => kim smith 

i came following regular expression i'm not getting matches:

string.scan(\([a-z \s*]{1,18})(^?!(\([a-z]{1,3}\)))\) 

i been banging head on while if can point error i'd appreciated it.

update:

i've gotten great responses, however, far none of regular expression solutions have met constraints. tricky part seems of string has country in parenthesis , don't. in 1 case strings without country not being matched , in returning correct string along country abbreviation without parenthesis. (see comments on second response.) 1 point of clarification: of strings matching start point of string. not sure if helps or not. again help.

the biggest error wrote (^?!...) meant (?=...). former means "an optional start-of-line anchor, followed !, followed ..., inside capture group"; latter means "a position in string followed ...". fixing that, makin few other tweaks, , adding requirement initial string end letter, get:

[a-z\s]{1,17}[a-z])(?=\s*\([a-z]{1,3}\) 

update based on op comments: since match @ start of string, can use \a "anchor" pattern start of string. can rid of lookahead assertion. this:

\a[a-z][a-z\s]{0,16}[a-z] 

matches start-of-string, followed uppercase letter, followed 16 characters either uppercase letters or whitespace characters, followed uppercase letter.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -