3

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?

Community
  • 1
  • 1
Piotr_iOS
  • 129
  • 1
  • 7
  • 1
    What is the regex flavor? If it is a regular regex engine, use `\/\/[ ]*MARK[ \t]*:*[ \t]*(?:(?!ABCD)[^\n])*$`. Is it for iOS? :) – Wiktor Stribiżew Mar 04 '16 at 12:14
  • It is used to rise warnings on specific marks using SwiftLiint. I will check the answers and write if they work:) – Piotr_iOS Mar 04 '16 at 12:40

1 Answers1

1

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.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563