replace multiple urls
-
hi
i have +1000 lines and want to convert any EXAMPLE.COM to EXAMPLE.COM/NAME1 using notepad++ or any other method:
<a href=“http://EXAMPLE.COM/” class=“mycode_url”>NAME1</a>
<a href=“http://EXAMPLE.COM/” class=“mycode_url”>NAME2</a>
…
TO
<a href=“http://EXAMPLE.COM/NAME1” class=“mycode_url”>NAME1</a>
<a href=“http://EXAMPLE.COM/NAME2” class=“mycode_url”>NAME2</a>
… -
So I’d advise you to learn about something called “Regular expressions”. Google it. Learn it. Because you know this type of problem will come up again for you soon, right? If you learn how to catch fish, you can feed yourself for a lifetime. But I’ll help you get started, and feed you for today! :-D
To solve your current problem, try this:
Find what zone:
(<a href="http://EXAMPLE\.COM/)(" class="mycode_url">)(NAME.)(</a>)
Replace with zone:\1\3\2\3\4
Search mode:Regular expression
Explanation of today’s feeding: This is a fairly simple use of regular expressions. In the Find-what expression, you’ll notice that I added 4 pairs of parentheses. These are not taken as literal text to match, but rather tell the search engine to remember what is matched in the parentheses for later use. The match contents of the first parentheses is remembered as group #1, the second as #2, etc. At replacement time,
\1
refers to what actually matched group #1,\2
is what matched group #2, and so on. Thus you’ll see that in this specific case, all we do is insert an extra group #3 copy in between group #1 and group #2.Note: So this solution is based upon your example. Since you supplied very little data, it is likely that this solution won’t exactly work for your real data. It certainly works for the 2 example lines you supplied, but maybe those aren’t truly representative of the full solution you need. If this is the case, this is where the learning comes in. You can adapt what I’ve supplied you to get your ideal solution, perhaps after doing some reading online about how this type of search and replacement works.
Final note: Be careful of double-quote characters if you copy and paste the above Find-what zone text…there are different types of these characters (look closely at them) and if you don’t have the right one, you won’t get a match.
-
I forgot to say that there is one more little nuance to the regular expression used above. The
.
inNAME.
will cause the find to match any single character. This is needed because in the two original lines one containsNAME1
and the other containsNAME2
. Perhaps more accurate would have been to use\d
instead of.
in the regular expression.\d
will match any single-digit number, i.e., 0-9. If your numbers are longer than one digit, you should use\d+
instead. The+
means “one or more of the previous”. This can get complicated somewhat quickly. Again, reading about it makes what seems quite difficult (at first) not too terrible to learn. -
@Scott Sumner
it worked just made a little change to the first expression
Find what zone: (<a href=“http://EXAMPLE.COM/)(” class=“mycode_url”>)(.*?)(</a>)
Replace with zone: \1\3\2\3\4
thank you
i appreciate your help