1

I'm using notepad++ I'm wondering if it's possible to replace every 3rd and 4th iteration in lines of HTML.

The original code looks like:

<div class="ui-bar ui-bar-d"
<div class="ui-bar ui-bar-d"
<div class="ui-bar ui-bar-d"
<div class="ui-bar ui-bar-d"

reoccurring...

I need to change every second block of two to look like this.

<div class="ui-bar ui-bar-d"
<div class="ui-bar ui-bar-d"
<div class="ui-bar bg ui-bar-d"
<div class="ui-bar bg ui-bar-d"

reoccurring...

Are there any regular expressions that will do this?

1 Answer 1

2

I assume your HTML looks like

<div class="ui-bar ui-bar-d" ... and something else ... (newline)
<div class="ui-bar ui-bar-d" ... and something else ... (newline)
<div class="ui-bar ui-bar-d" ... and something else ... (newline)
<div class="ui-bar ui-bar-d" ... and something else ... (newline)
... etc...

and assume that you wish to keep the ... and something else ... part.

At search field, try

(<div class="ui-bar ui-bar-d".*\r\n)(<div class="ui-bar ui-bar-d".*\r\n)<div class="ui-bar ui-bar-d"(.*\r\n)<div class="ui-bar ui-bar-d"(.*\r\n)

Replace with

\1\2<div class="ui-bar bg ui-bar-d"\3<div class="ui-bar bg ui-bar-d"\4

Depending on whether you are using Windows or UNIX type EOL (i.e. newline character), you may need to change all \r\n into \n.


A more generalized regex that replace only ui-bar ui-bar-d clause:

(.*ui-bar ui-bar-d.*\r\n)(.*ui-bar ui-bar-d.*\r\n)(.*)ui-bar ui-bar-d(.*\r\n)(.*)ui-bar ui-bar-d(.*\r\n)

and replace with

\1\2\3ui-bar bg ui-bar-d\4\5ui-bar bg ui-bar-d\6
3
  • I really appreciate the reply, it looks like exactly what I need, although I can't get it to work for love nor money, I tried a few variations of your suggestion but to no avail. I just get the "Can't find text" error when I try to replace. I'm using I'm using windows and you're correct in saying there's more to the HTML. An example of a full line looks like <div class="ui-block-a"><div class="ui-bar ui-bar-d" style="">Superuser</div></div>
    – N0xr-2
    Apr 10, 2014 at 5:13
  • So there's an additional string (<div class="ui-block-a">) on each line before the pattern. You will need to modify the search pattern in that case; I revised my answer to make the search pattern more general.
    – Kenneth L
    Apr 10, 2014 at 6:32
  • Thanks a lot, this answers my question and helps me to understand how regex syntax works.
    – N0xr-2
    Apr 11, 2014 at 3:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.