scotilen
scotilen

Reputation: 43

Regex to match all but a combination of characters

I am in love with Regex but there is a 'simple' thing I am not able to do. I have been struggling with this for a while (and it's been fun!), so I thought some help would be very sweet. Thanks for already reading this plea!

I have the following string that I would like to match some parts of it:

this is cool--this is nice--this-is-it

I would like a regex that would returns three matches, being them:

this is cool

this is nice

this-is-it

I am trying to match any texts separated by the double dash (--), however, the single dash should be treated as if it were any other character. So far I have tried a few things, but the closer I got was this regex: /[^(--)]+/gm. That regex is currently returning 5 matches which can be visualized here:

https://regex101.com/r/W58b5W/1

Thank you!

Upvotes: 1

Views: 1786

Answers (2)

Hao Wu
Hao Wu

Reputation: 20669

[^(--)] does not mean what you think it means. Anything inside of [...] only matches one character, hence [^...] can not exclude any combination has more than 1 character.

What you would need are negative lookarounds or control verbs.

For your specific case(assuming there are no more than 2 -s in a row or -- appears at the beginning or the end of the string), you just need to match anything in between -- or start and end of the string, so it can be written as

(?<=--|^).*?(?=--|$)
  • (?<=--|^) positive lookbehind, make sure the start of the match is either a -- or the beginning of the string.
  • .+? match any characters, as fewer as possible.
  • (?=--|$) positive lookahead, make sure the end of the match is either a -- or the end of the string

Check the test cases


But notice for some regex engine such as Safari is using does not support lookbehinds, so you need to make the lookbehind an actual match

(?:--|^)(.*?)(?=--|$)

Then you can get the results from group 1.

Upvotes: 2

Ali Samji
Ali Samji

Reputation: 479

(.+?)(?:--|$)

Since it seems you want to use regex to split your string, what about trying to find all lengths of strings that end with -- or the end of string ($). These will give you a list of all the matches with the string you desire in the first capture group which you should be able to access.

However, if you just need to split the string by a single delimiter (i.e. --), you should use your programming languages string split ability. Regex is beneficial in this case if you need to split using multiple delimiters (e.g. -- or || or ::)

Upvotes: 1

Related Questions