user2520444
user2520444

Reputation: 11

Multiple find and replace in notepad++

I'm using regular expression in Notepad++ to find and replace <span class="bold">(.*?)</span> with <strong>\1</strong> and <span class="italic">(.*?)</span> with <i>\1</i>. I have to do this to a lot of documents and want to know if I can accomplish both of these using a single find and replace.

Upvotes: 1

Views: 658

Answers (2)

hwnd
hwnd

Reputation: 70722

You may consider using sed to accomplish this task using a single command line. The below example will find/replace multiple patterns/replacements in all .txt files in a given directory.

sed -e 's/pattern1/replacement1/g;s/pattern2/replacement2/g' *.txt

To actually replace those patterns, use the i option. The -r option allows extended regular expressions.

sed -i -re 's!<span class="bold">(.*?)</span>!<strong>\1</strong>!g;s!<span class="italic">(.*?)</span>!<i>\1</i>!g' *.txt

Upvotes: 3

Sam
Sam

Reputation: 20486

I came up with somewhat of a hack to make this work, however it can only make <span class="bold"> into <b> not <strong> because it captures a character from the class:

<span class="(b(?=old)|i(?=talic))[^"]+">(.*?)<\/span>
<\1>\2</\1>

Demo


Explanation:

<span class="
(             (?# start capture group for new element)
  b           (?# match b...)
  (?=old)     (?# followed by old)
 |            (?# OR)
  i           (?# match i)
  (?=talic)   (?# followed by italic)
)             (?# end capture group)
[^"]+         (?# match non-" characters that were found in lookaheads)
">
(.*?)         (?# lazily capture the contents of the span)
<\/span>

But you should be able to find/replace in all files with Notepad++...

Upvotes: 1

Related Questions