2

In Visual Studio 2013, I can find a tag like:

<div class="old-title">Different Title for every page</div>

Using

(?i)<div class=(\"old-title\").(?i)(.+?)./div[^>]*>

To replace with

<div class="title">$2</div>

I need to search and find multiple lines

                    <div class="class1">
                        <div class="class2"></div>
                        <div class="title">Different Title for every page</div>
                    </div>

And replace with only single div title:

<div class="title">$2</div>
hsobhy
  • 1,493
  • 2
  • 21
  • 35
  • is this what your looking for? http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Albert Laure Apr 21 '14 at 07:29
  • @hshobby I fixed a typo (matching the literal "different title"—duh) and added explanations. Should work now, let me know. – zx81 Apr 21 '14 at 07:40

1 Answers1

4

hshobby, if I understand, your question is about the multiple-line replacement at the bottom. Is this what you are looking for? (If the title also need to be captured, that is an easy tweak.)

Search:

(?is)<div class="class1">[\s\r\n]*<div class="class2"></div>[\s\r\n]*<div class="title">([^<]+)</div>[\s\r\n]*</div>

Replace:

<div class="title">$1</div>

If so, here are some differences with your original expression. First off we are allowing the dot to match new line characters so that the regex can span several lines. This is what (?s) does. Since you were turning on case-insensitive mode later on (?i), it seemed cleaner to bring it together at the front: (?is) One repeated piece here is the [\s\r\n]* character class, which eats up any spaces and new lines, allowing us to reach the next part of the string you are trying to match.

Finally, the title itself is matched and captured with [^<]+ which says one or more character that is not a <, allowing us to consume all the characters before </div> If a title can be empty, replace [^<]+ with [^<]*

Input:

<div class="class1">
                        <div class="class2"></div>
                        <div class="title">Hi there hsHobby</div>
                    </div>

Output:

<div class="title">Hi there hsHobby</div>
zx81
  • 41,100
  • 9
  • 89
  • 105