1

Hi guys I really need some help, I need to do a mass replace expression in files I have a large list of urls which needs to be replaced.

I want to search files and replace each with the appropriate brand anchor link e.g.

http://www.example.com becomes

<a href=”http://www.example.com”> http://www.example.com</a>

I need to do this with a large list of urls in multiple files

I tried the following expression (1)|(2)|(3) (?1a)(?2b)(?3c) But It doesn’t work. This is beyond me. Any help would be appreciated. Thanks

1 Answers1

2

Go to Search > Replace menu (shortcut CTRL+H) and do the following:

  1. Find what:

    http:\/\/www\.\w+\.com
    
  2. Replace:

    <a href="$0">$0</a>
    
  3. Select radio button "Regular Expression"

  4. Then press Replace All in All Opened Documents

You can test it and see the results at regex101.

Important note: matching URLs with regular expressions can be complicated! I gave you the simplest example matching only URLs like http://www.example.com. If you have more complicated stuff, let us know but showing some of your data! More info on this matter here and here.

UPDATE:

Let's make it slightly more complicated to match also yoursite.com/index.php?remainingurl

Find what:

    (?:https?:\/\/)?(?:www\.)?(\w+\.\w{2,6})(?:\/\w+\.\w+(?:\?\w+)?)?\b

Replace:

    <a href="$0">$1</a>
Community
  • 1
  • 1
psxls
  • 6,807
  • 6
  • 30
  • 50
  • 1
    Nice answer. Just to add my contribution : $0 contains the whole matched string. If you have to get a substring, capture with parenthesis and use $1, $2...$N tokens. For example here if you only want to get the url domain (for text). Regex : http:\/\/www\.(\w+)\.com => $1 – OlivierH Nov 12 '13 at 18:55
  • Hi thanks for your help, great answer. I’ve managed to get it working because it’s a number of specific urls in a large number of documents I still use the (site1)|(site2)|(site3)… In the find field and replace with $0 as you suggested and it worked. Thanks a lot, appreciated. – user2984504 Nov 13 '13 at 06:01
  • I have encountered a small problem, it works fine until there is a website with a php url with a question mark. For example http://www.yoursite.com/index.php?remainingurl any ideas how to find and replace these with a similar expression? Thanks – user2984504 Nov 13 '13 at 15:18