Chris Barrett
Chris Barrett

Reputation: 601

REGEXEXTRACT before a character

I'm trying to write a small regex to extract the text before an optional (.

I have this:

^(.*)[\(.*]|$

But its not working for some reason. Doesn't seem to make it to the $ if there is no ( present.

Any help would be much appreciated

Cheers

Upvotes: 2

Views: 2335

Answers (1)

The fourth bird
The fourth bird

Reputation: 163362

Your regex will either capture 0+ times any character in a capturing group (.*) followed by a character class matching one of the listed characters [\(.*], or it will match an empty string due to the alternation |$.

If the first part of the alternation does not match a character from the character class at the end, you will not have a match.

You could use a negated character class to match not a ( from the start of the string:

^[^(]+

Upvotes: 1

Related Questions