Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx match content inside div with specific class

Tags:

html

regex

How can I match the contents of all divs that have a specific class. For example:

  <div class="column-box-description paddingT05">content</div>
like image 736
Florian Shena Avatar asked Mar 30 '14 11:03

Florian Shena


2 Answers

Generally, you shouldn't do this with regex unless you can make strong assumptions about the text you're matching; you'll do better with something that actually parses HTML.

But, if you can make these stronger assumptions, you can use:

<div class="[^"]*?paddingT05[^"]*?">(.*?)<\/div>

The key part is the reluctant quantifier *? which matches the minimal text possible (i.e. it doesn't greedily eat up the </div>.

like image 107
beerbajay Avatar answered Sep 20 '22 18:09

beerbajay


You can do something like this:

<div.*class\s*=\s*["'].*the_class_you_require_here.*["']\s*>(.*)<\/div>

Replace "the_class_you_require_here" with a class name of your choosing. The div content is in the first group resulted from this expesion. You can read on groups here: http://www.regular-expressions.info/brackets.html

like image 40
ed22 Avatar answered Sep 18 '22 18:09

ed22