0

I'm trying to put a wrapper around a bunch of fields that already exist.

I want to convert this:

<div class="editor-label">
    @Html.LabelFor(Function(model) model.FirstName)
</div>
<div class="editor-field">
    @Html.EditorFor(Function(model) model.LastName)
</div>

To this:

<div class="field-group">
    <div class="editor-label">
        @Html.LabelFor(Function(model) model.FirstName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(Function(model) model.LastName)
    </div>
</div>

I'm pretty green with Regex, but here's the closet I came so far to matching on the pattern:

<div class="editor-label">(.|\r\n)*?(</div>)(.|\r\n)*?(</div>)

Then it could replace with something like this:

<div class="field-group"><div class="editor-label">(.|\r\n)*?(</div>)(.|\r\n)*?(</div>)</div>

But that obviously doesn't work. But the idea would be wrapping around the matched value.

When I use the replace in Notepad++, it replaces it with the string (.|\r\n)*?(</div>) instead of the matched value.

Can I do a wildcard search and replace that persists the original wildcard matched value?
Or somehow wrap the contents another way?

Note: I'm using Notepad++, but I'd gladly use any other text editor that has this kind of functionality.

5
  • Notepad++, right? Not the stock Notepad included in Windows? Sep 18, 2014 at 22:31
  • What are you replacing the match with? That seems to be the problem.
    – Excellll
    Sep 19, 2014 at 19:57
  • @Excellll, I want to basically add a wrapper around content that matches the provided description. If it matches, I want to take the whole text block and add content before and after it.
    – KyleMit
    Sep 19, 2014 at 20:17
  • Got it. I was more interested in the string you are using to replace the match in Notepad++. That is, what do you have typed in the Replace with field?
    – Excellll
    Sep 19, 2014 at 20:20
  • @Excellll, added update
    – KyleMit
    Sep 19, 2014 at 20:32

1 Answer 1

1

You can do this with the regex substitution engine which is exposed through the Find and Replace feature in notepad. But first, you'll need to be a little familiar with how any regex is able to find and replace text.

Some Regex Info

Capturing Groups:

  • (ABC) - capturing group - Groups multiple tokens together and creates a capture group
  • (?:ABC) - non-capturing group - Groups tokens together without creating a capture group
  • \n - backreference - Matches the results of a previous capture group (ex. \1)

Substitution Symbols:

  • $& - match - Inserts the entire matched text.
  • $n - capture group #n - Inserts the results of the specified capture group (ex. $1)

How to Wrap with Regex Substitution

To wrap any text, first right a regex query to find it. Then replace that entire string with a new one composed of the text you'd like to wrap around it and the original matched text by using $&.

Here's a Demo in RegExr:

Wrap Text

Here's an Example with JavaScript:

var wrapMe = 'WrapMe';
var wrapped = wrapMe.replace(/WrapMe/g, "<wrapper> $& </wrapper>");
console.log(wrapped); // <wrapper> WrapMe </wrapper>

To wrap the entire regex query with a substitution


How to Insert with Regex Substitution

Here's a Demo in RegExr:

Insert Text

Here's an Example with JavaScript:

var insertMe = '<wrapper>Replace</wrapper>';
var inserted = insertMe.replace(/(<wrapper>).*(<\/wrapper>)/g, "$1 Inserted $2");
console.log(inserted); // <wrapper> Inserted </wrapper>

The Problem at Hand

Knowing all that, the problem at hand in Notepad (or anywhere else) should seem rather trivial:

Here's a Demo in RegExr:

Search For: <div class="editor-label">((.|\n)*?(<\/div>)){2,2}
Replace With: <div class="field-group">\n$&\n</div>

You can pull up the Find and Replace with Ctrl + H

Problem Screenshot

Note: Remember to set the search mode as Regular Expression and also that notepad typically requires \r\n to find a newline instead of just \n

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .