Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch any character (including \n) in Visual Studio Regex

What I want to do:

I want to reformat a bunch of similarly written HTML files in Visual Studio. The html parts may have a different number of lines, but I know that they start with a <div id="some_id"> tag and there is no other <div> tag enclosed. So how would I write a regex that catches any number of lines in between?

What I tried so far:

I can't use the ., because it doesn't catch line breaks. But I also don't know how to tell Visual Studio to catch either \n or .. I know how to use sets (like [a-z0-9]) but as soon as you put a . into them it turns into a literal. So <div id="some_id">[.\n]*?</div> catches only divs which contain a bunch of dots and new lines

Thanks in advance

like image 732
Neuron Avatar asked Mar 09 '23 00:03

Neuron


1 Answers

In Visual Studio Find and Replace feature, you may match any chars including line breaks by using a [\s\S\r] character class. For some reason, [\s\S] does not match the CR (carriage return) symbol.

Your [.\n] char class only matches a literal . or a newline.

Thus, use

<div id="some_id">[\s\S\r]*?</div>
                  ^^^^^^^^^^
like image 109
Wiktor Stribiżew Avatar answered Apr 01 '23 01:04

Wiktor Stribiżew