Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex needed to match anything within p tags

Tags:

regex

I need a regular expression to match anything that is within <p> tags so for example if I had some text:

<p>Hello world</p>

The regex would match the Hello world part

like image 571
geoffs3310 Avatar asked Feb 03 '11 08:02

geoffs3310


People also ask

Does regex match anything?

In regular expressions, we can match any character using period "." character. To match multiple characters or a given set of characters, we should use character classes.

What is difference [] and () in regex?

[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.


1 Answers

in javascript:

var str = "<p>Hello world</p>";
str.search(/<\s*p[^>]*>([^<]*)<\s*\/\s*p\s*>/)

in php:

$str = "<p>Hello world</p>";
preg_match_all("/<\s*p[^>]*>([^<]*)<\s*\/\s*p\s*>/", $str);

These will match something as complex as this

< p style=  "font-weight: bold;" >Hello world  <  /  p >
like image 153
xzyfer Avatar answered Sep 27 '22 20:09

xzyfer