Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] Regular Expresssions


I have a general question about Regular Expressions.


I am using Notepad++ and I want to replace a line for another in 100+ opened documents.


This is what I have:


<p class="block3"><img alt="Image" class="calibre6" src="../Images/image-11.jpeg" />


I want to replace it to something like this:


<img alt="Image" class="calibre6" src="../Images/image-11.jpeg" />


Q: how do I keep the img tag as is and remove the p tags in all opened documents (notice that the img class changes in each document)?


P.S: I used (<p class="block)[0-9]*(">) to capture the opening p tags



<div> </div>
<div>Assuming I'm reading this correctly, you want to strip the tags surrounding the <img> tag, right?  If that's the case, then you really only need to capture the <img> tag.</div>
<div> </div>
<div>This particular regular expression may do what you want...note that this uses a quick and dirty method of picking up html tags, and would fail if there were another ">" character inside the tag.</div>
<pre class="_prettyXprint">
If this doesn't quite work for you, then you may want to try experimenting with ...I've found the highlighting to be quite helpful.


<div>Hopefully that helped. Smile</div>
Yes I do want to strip those tags away but I don't see your implementation works.


I do:


1. Find: <span style="color:rgb(0,0,136);"><p</span><span>[^</span><span style="color:rgb(0,0,136);">></span><span>]*>(</span><span style="color:rgb(0,0,136);"><img</span><span>[^</span><span style="color:rgb(0,0,136);">></span><span>]+\/>)<\/p></span>

<span>2. Replace with: ""</span>


<span>That would strip away those tags? no, it would simple find all those lines and replace them with nothing. if I put "</span>(<span style="color:rgb(0,0,136);"><img</span>[^<span style="color:rgb(0,0,136);">></span>]+\/>)" in the replace area it would know what to replace with?

Try replacing it with "$1".


You will not be able to use regular expressions if the html gets more complex though. In that case you would need a DOM parser, such as the one found in your browser, or by making for example a simple node.js script.


<p class=(.*)><img alt=(.*) class=(.*) src=(.*) />



<img alt=\2 class=\3 src=\4 />

That fails if you have multiple per line though, because * is greedy. In that cause you could use instead:

<p class=(.*?)><img alt=(.*?) class=(.*?) src=(.*?) />

Sweet as! that helped a lot, thanks everyone!


EDIT: Bargeld, only $1 keeps the original and replaces the other tags you choose, thanks.


Forum Jump:

Users browsing this thread: 1 Guest(s)