3

Ok i have this multiple static html and on each html urls are accompanied with underscore i want to replace all url that has underscore with dash here are the actual codes

1)

<a href="/category/how_to_tell_your_dad_that_you_are_pregnant/index.html">How to tell my dad that i am pregnant</a>

2)

< href="/premarital_sex/index.html">Premarital Sex</a>

should be

1)

<a href="/category/how-to-tell-your-dad-that-you-are-pregnant/index.html">How to tell my dad that i am pregnant</a>

2)

< href="/premarital-sex/index.html">Premarital Sex</a>

i want to replace all URL's on all of my static html how can i do that? or is it even possible?

i tried htaccess but no luck so i'll just have to use notepad++ instead or powergrep but i am not really sure how or where to start :(

thanks in advance guys

EDIT

I guess I'm near:

(href=")*[_](.+?">)
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
mark ong
  • 33
  • 3

2 Answers2

3

You can easily work around the problem of a missing infinite width lookbehind in Notepad++ and Sublime Text with the help of \G operator.

Use

(\bhref="|(?!^)\G)[^"<_]*\K_

And replace with -.

enter image description here

Explanation:

  • (\bhref="|(?!^)\G) - define the leftmost boundary for a match: a whole word href=" should be matched (\b is a word boundary) or match the position at the end of the previous successful match (with (?!^)\G)
  • [^"<_]* - match 0+ characters other than ", < and _
  • \K - omit the whole text we matched so far from the buffer
  • _ - match only this character - and only this will be replaced.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
1

You need another editor that supports variable length lookbehinds. NP++ doesn't.

So, with a smart editor, sublime probably, you could do this: in one go: Find for this: (?<=href="[^"]*)_(?=[^"]*") and replace them with this: -.

In notepad++ you can do this instead: Find this: (href="[^"]*)_(?=[^"]*") and replace them with this: \1-. But you'll have to click Replace All several times. I.e. the url having the most underscores has 10, then you have to click it 10 times.

Tamas Rev
  • 7,008
  • 5
  • 32
  • 49