Need help stopping at parenthesis

Viewed 22

I'm new with regex and really need some help here. I'm trying to create a regex that finds everything before the first space and open parenthesis in the example below. Basically, I'm trying to just keep the country name or names and exclude everything after.

Falkland Islands (Malvinas)

I tried this but it isn't working:

(\w+)(?=[\s+(\w+\s+])
1 Answers

Use

^.*?(?=\s*\()

See proof here.

Explanation

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  .*?                      any character except \n (0 or more times
                           (matching the least amount possible))
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    \(                       '('
--------------------------------------------------------------------------------
  )                        end of look-ahead
Related