Piotr_iOS
Piotr_iOS

Reputation: 129

Regex - sets subtraction

I have to find all strings starting with //MARK that do not contain sting ABCDS. I had some trials but all failed. Biggest question here is to mark set A-B.

(\/\/[ ]*MARK[ \t]*[:]*[ \t]*[^\n(?P=ABCD)]*)

It should work with:

//MARK: MarABdasdsd
//MARK sthIsHere

But should not match:

//MARK: great marABCDE

I am able to find all cases but do not know how to remove this one. I can use only single regular expression. I am aware of many posts negate the whole regex pattern when negative lookaround doesn't work

Any ideas?

Upvotes: 3

Views: 933

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627498

I assume you are coding in Swift that uses ICU regex flavor. It supports lookaheads, thus, the regex based on a tempered greedy token will work:

//[ ]*MARK[ \t]*:*[ \t]*(?:(?!ABCD)[^\n])*$

See the regex demo

The regex matches

  • // - two /
  • [ ]* - 0+ spaces
  • MARK - a literal word MARK
  • [ \t]*:*[ \t]* - 0+ spaces or tabs followed with 0+ colons followed with 0+ tabs or spaces
  • (?:(?!ABCD)[^\n])* - the tempered greedy token matching any non-newline symbol that does not start a ABCD sequence
  • $ - end of string.

Upvotes: 1

Related Questions