CodeGrabber
{ USER }
posts: 23
last: 28-Apr-2008
TITLE: Match an html attribute
DESCRIPTION: The goal of this expression is to match all id attributes of the div tags on a page
Submitted: 28-Nov-2007 04:41:44 ( 1yrs 5w 6d 2h ago ) Language: REGEX (*.)
Views: 211 Lines of Code: 26 LINES
Rating:
rate: star1
star2
star3
star4
star5
dstar1
dstar2
dstar3
dstar4
dstar5  ( rated! )
  { 0.00 / 5 }
Difficulty: Intermediate
Bookmark
<div(?=\s)
(?:
  (?!\sid=|>)
  .
)* # Consume everything until finding " id=" or ">"
   # (">" is just for failing faster)

\sdiv=(?P<__quote>['"])? # Consume " div=" and save the quote type (' or ") if any

(?: # while
  (?! # next character isn't, 
    (?(__quote) # if a quote has been opened,
      (?P=__quote) # the closing quote ;
      |[\s>] # a space or ">" else,
    )
  )
  . # consume this character
)*


(?(__quote) # If we got a quote
  (?P=__quote)| # it must be closed
  (?=[\s>]) # else, the attribute is ended by a space or ">"
)

[^>]*> # Consume the rest of the tag