Grab string until one two conditions

Viewed 36

Let's say I have the following two strings:

...some content above
Destination:        Fox - Man (200.170.217.200)
...some content below

And

...some content above
Destination:        Some content goes here and ends
...some content below

The regex should grab:

Fox - Man and Some content goes here and ends.

I would like to grab everything in the description except a trailing parenthetical. Basically, it is combining these two regexes:

Destination:\s*(.+\S)    # everything until the end of the line
Destination:\s*([^(]+\S) # everything until the first open parenthetical

How would I combine these so that the exit at ( takes precedence? Sample here: https://regex101.com/r/5qUwRW/1

1 Answers

Use

Destination:\s*([^(\n]+[^\s(])

See proof

Explanation

--------------------------------------------------------------------------------
  Destination:             'Destination:'
--------------------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    [^(\n]+                  any character except: '(', '\n'
                             (newline) (1 or more times (matching the
                             most amount possible))
--------------------------------------------------------------------------------
    [^\s(]                   any character except: whitespace (\n,
                             \r, \t, \f, and " "), '('
--------------------------------------------------------------------------------
  )                        end of \1
Related