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

 

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

 

Thanks,

Reply
#2
<div>Hi,</div>
<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>
<div>
<pre class="_prettyXprint">
<p[^>]*>(<img[^>]+\/>)<\/p></pre>
If this doesn't quite work for you, then you may want to try experimenting with http://regexpal.com/ ...I've found the highlighting to be quite helpful.

 

</div>
<div>Hopefully that helped. Smile</div>
Reply
#3
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?

Reply
#4
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.

Reply
#5
Find:

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


 

Replace:

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

Reply
#6
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=(.*?) />

Reply
#7
Sweet as! that helped a lot, thanks everyone!

 

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

Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)