Reputation: 647
I am not very good at regular expression. I was trying to replace a text in Netbeans for a large HTML document. There are several tags like these:
<canvas width="62" height="23" style="width: 62px; height: 23px; top: 1px; left: 1px; ">
<canvas width="62" height="23" style="width: 62px; height: 23px; top: 1px; left: 1px; ">
<canvas width="67" height="23" style="width: 67px; height: 23px; top: 1px; left: 1px; ">
I want to replace these tag with a space or null value to remove them.
I tried with
^<canvas width="[0-9]*" height="[0-9]*" style="width: [0-9]*px; height: [0-9]*px; top: [0-9]*px; left: [0-9]*px; ">
but it didn't help.
Can anyone give me a solution?
Upvotes: 3
Views: 8113
Reputation: 647
I have found the solution.
I tried with this and it worked fine.
<canvas (?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>
It can be used for any html tag in place of "canvas" in my case.
Upvotes: 0
Reputation: 13169
I think your regular expression looks fine except for the start-of-line boundary matcher ^
which forces the search to begin at the start of the line. So if your targeted tags do not begin a line, they won't be found by the matcher.
If your targeted tags can be found anywhere in the document, and your regular expression has no chance of matching anything you want to keep, get rid of the ^
boundary matcher and then test to see that it works by using "Find" before you use "Replace" or "Replace All".
Upvotes: 0
Reputation: 6045
Your expression only works if the line has no preceeding whitespaces. Use this expression instead:
^[ \t]?<canvas width="[0-9]*" height="[0-9]*" style="width: [0-9]*px; height: [0-9]*px; top: [0-9]*px; left: [0-9]*px; ">
[edit] If the expression is preceeded by text you need to remove the caret (^) in the beginning.
Upvotes: 0
Reputation: 5676
Using regular expressions to parse html is bad idea, but if you have to...
Try using regex groups:
^<canvas width="([0-9]*)" height="([0-9]*)" style="width: ([0-9]*px); height: ([0-9]*px); top: ([0-9]*px); left: ([0-9]*px); ">$
So you can refer to first group by $1 etc.
Remember about multiline flag if you are using ^ and $
Upvotes: 8