harryz
harryz

Reputation: 5260

Regex: How to capture multiple lines of contents

When resolving git conflicts, I am trying to use regex to capture conflicts from source file, the content is like this:

<<<<<<< HEAD
    line1
    line2
    ... 
=======

This is my version:

^<<<<<<< HEAD\n\t.+\n\t.+\n\t=======\n\t  

Obviously, my version only works for fixed lines because I need to repeat .+\n\t to match them. But really no idea of how to do this properly? Any tips?

[Update] I am using Perl Compatible Regular Expressions (PCRE) engine from the Boost library.

Upvotes: 1

Views: 3041

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626689

You may use

(?sm)^<<<<<<< HEAD.*?\n\t*=======\n*\t*

See the regex demo

Explanation:

  • (?sm) - inline modifiers: s makes the . match a newline as well as any other character and m makes the ^ match the line start and $ match the line end
  • ^ - start of the line
  • <<<<<<< HEAD - a literal string <<<<<<< HEAD
  • .*? - 0+ any characters, as few as possible, up to the first
  • \n\t* - LF followed with 0+ tabs
  • ======= - a literal substring
  • \n*\t* - (not sure you really need it) 0+ LFs followed with 0+ tabs.

Upvotes: 3

MoustafaS
MoustafaS

Reputation: 2031

What language do you use ? The option is allowing the "." to match new line along with ANY char, so that it matches multi line strings.

Adding this require the knowledge of the language in which you code.

Upvotes: 0

Related Questions