Home > OS >  RegEx - How to find all occurrences, including newlines, between two points?
RegEx - How to find all occurrences, including newlines, between two points?

Time:08-15

Assume I have

    changed: true
    newTranslated: ''
    importedTokens: true

and (notice there is no ' ' here)

    changed: true
    newTranslated: 
    Some cool text here
    Here is some more next
    importedTokens: true

and

    changed: true
    newTranslated: '|cFFASD2Hello there!'
    importedTokens: true

I want to search everything, including newlines between changed: true and importedTokens (this includes changed: true, but not importedTokens). I also do not want to match newTranslated: ''

With this RegEx, I match changed: true and any newTranslated, except (NewTranslated: '').

changed: true\n.*^((?!'').)*$

but only the first line of newTranslated it fetches, I want it to keep going until it finds "importedTokens", but does not include it in the match

CodePudding user response:

You might use a pattern with a capture group and a negative lookahead:

\bchanged:\s*true((?:\n(?!\s*\b(?:newTranslated:\s*''|importTokens:|changed:)).*)*)(?=\n\s*\bimportedTokens:)

The pattern matches:

  • \bchanged:\s*true Match the word changed and then : optional spaces and true
  • ( Capture group 1 (that has the data you are interested in)
    • (?: Non capture group to repeat as a whole part
    • \n Match a newline
    • (?!\s*\b(?:newTranslated:\s*''|importTokens:|changed:)).*)* Match the whole line if is does not contain any of the alternatives
  • ) Close group 1
  • (?=\n\s*\bimportedTokens:) Positive lookahead, assert a newline and importedTokens:
  • Related