Jelle De Loecker
Jelle De Loecker

Reputation: 21955

Stop match at first occurence of character, or continue

I have input like this:

I want to get everything before the first parens, or just everything if there are none

I've tried this:

(.+)(?=\()

But that only works when there are parens, obviously. Then I tried this:

(.+)(?=\()?

But that just selects everything. Does anyone have a solution?

Upvotes: 0

Views: 77

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174706

You could try the below regex.

^[^(]*

^ Asserts that we are at the start and [^(]* matches any character but not of ( zero or more times. * repeats the previous token zero or more times.

DEMO

Upvotes: 1

nu11p01n73R
nu11p01n73R

Reputation: 26667

You can use a very simple regex as

^[^(]+
  • ^ Anchors the regex at the start of the string.

  • [^(] Matches anything other than ( . Quantifier + matches one or more occurence of the presceding regex

Regex demo link

Upvotes: 2

Related Questions