tags:

views:

29

answers:

2

I need a regex to match all self-closing <input /> tags which lacks a type attribute.

For example, I want to find these:

<input size="1" />
<input name="test" />

But not this:

<input type="radio" />

Please note, this should be adaptable for any single attribute. I am just using type here as an example.

FYI, I am performing a search across 1000s of .html files using AstroGrep.

You can assume that the attribute is well-formed, with equals sign and double quotes.

A: 

I don't know what is AstroGrep, but if it has negative look-ahead, you could just do

(?!\<input(?:[[:space:]]+[a-zA-Z0-9_]+="[^"]*")*(?:[[:space:]]+type="[^"]*"))<input(?:[[:space:]]+[a-zA-Z0-9_]+="[^"]*")*[[:space:]]*/>

Without it, it is much more laborious.

jpalecek
+1  A: 
<input(?:\s+(?!type=)\w+="[^"]*")*\s*/>

That should work if AstroGrep's regex flavor isn't too exotic. I can't find an online reference for it.

Alan Moore