Seriel Killer Posted November 30, 2013 Report Posted November 30, 2013 (edited) 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: ../Images/image-11.jpeg I want to replace it to something like this: ../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 () to capture the opening p tags Thanks, Edited December 3, 2013 by Seriel Killer
lightbender Posted November 30, 2013 Report Posted November 30, 2013 Hi, Assuming I'm reading this correctly, you want to strip the tags surrounding the tag, right? If that's the case, then you really only need to capture the tag. 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. <p[^>]*>(<img[^>]+\/>)<\/p>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. Hopefully that helped.
Seriel Killer Posted November 30, 2013 Author Report Posted November 30, 2013 (edited) Yes I do want to strip those tags away but I don't see your implementation works. I do: 1. Find: [^>]*>([^>]+\/>)<\/p>2. Replace with: "" That would strip away those tags? no, it would simple find all those lines and replace them with nothing. if I put "([^>]+\/>)" in the replace area it would know what to replace with? Edited November 30, 2013 by Seriel Killer
JoWie Posted November 30, 2013 Report Posted November 30, 2013 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.
JoWie Posted December 2, 2013 Report Posted December 2, 2013 That fails if you have multiple per line though, because * is greedy. In that cause you could use instead:(.*?)
Seriel Killer Posted December 3, 2013 Author Report Posted December 3, 2013 (edited) Sweet as! that helped a lot, thanks everyone! EDIT: Bargeld, only $1 keeps the original and replaces the other tags you choose, thanks. Edited December 3, 2013 by Seriel Killer
Recommended Posts