andrux
andrux

Reputation: 2922

Regex to match text between two strings, including the strings

I'm trying to fix some conflicts in a merge by git, there are a lot of <<<<<< HEAD and ====== blocks I want to be able to just find and replace with an empty string in a lot of files.

I found this regex pattern that correctly matches everything between the two strings, but it leaves out the beginning and ending strings, and I want to be able to match them also.

(?s)(?<=<<<<<<< HEAD).*?(?=\=\=\=\=\=\=\=)

So, match <<<<<<< HEAD, ======= and everything between them to do a search/replace.

Can anyone help me out? I would be running this on files I'm certain I don't want anything between those strings, I guess that's also why I didn't try a "use theirs" flag when doing the merge, because I need to see the files first.

Upvotes: 2

Views: 1336

Answers (3)

Devyn Goetsch
Devyn Goetsch

Reputation: 11

You want to match on the opening and closing tags of the conflict sections?

Parentheses are primarily used for group capturing, if you do like so:

(<<<<<<< HEAD)(.*\s)+(\s*=======)

It will create 4 groups which you can access the members.

Tested: http://regexr.com/

Upvotes: 0

10101
10101

Reputation: 329

Just leave out the look-arounds mentioned by Xufox

(?s)(<<<<<<< HEAD)(.*?)(\=\=\=\=\=\=\=)

The .*? is wrapped with parentheses so you can reference it in the replacement. \1 for the first group, \2 for the second, and \3 for everything in between (but the syntax can vary.)

Upvotes: 5

karina
karina

Reputation: 805

I think you might be asking the wrong question here. The best way to actually handle merge conflicts is with a merge tool. You should look into something like meld. And specifically setting git merge tool to use that. Manual merges are not fun...

Use a pretty ui to analyze the merge instead

Upvotes: 2

Related Questions