How can I select the tag-name and attributes AND values of those attributes with ONE regular expression?

StackOverflow https://stackoverflow.com/questions/913320

  •  06-09-2019
  •  | 
  •  

Question

I have the following Regular Expression from this post (Regular expression for extracting tag attributes).

(\S+)=["\']?((?:.(?!["\']?\s+(?:\S+)=|[>"\']))+.)["\']?

I've created the following PHP code and it works nicely. I get [id='gridview1' and 'id' and 'gridview1'] from the preg_match_all() function.

$regexp = '/(\S+)=["\']?((?:.(?!["\']?\s+(?:\S+)=|[>"\']))+.)["\']?/';
$text = '<asp:gridview id=\'gridview1\' />';

$matches = null;
preg_match_all($regexp, $text, $matches);

print_r($matches);

How should the regular expression be changed to also return 'asp' and 'gridview'? (or 'Foo' and 'bAR' when i use:

<Foo:bAR />

OTHER TIPS

([a-zA-Z]+)\:([a-zA-Z]+) would work for something like Foo:bar

<.*?([a-zA-Z])+.*?\:.*?([a-zA-Z])+.*?\/> would work for < Foo : BArrr />

Things can be optimized depending on your requirements and whether you know that a certain type of formatting is enforced.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top