Florian Shena
Florian Shena

Reputation: 1434

RegEx match content inside div with specific class

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

  <div class="column-box-description paddingT05">content</div>

Upvotes: 10

Views: 25949

Answers (2)

beerbajay
beerbajay

Reputation: 20270

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>.

Upvotes: 12

ed22
ed22

Reputation: 1237

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

Upvotes: 3

Related Questions