Eugen
Eugen

Reputation: 155

Regex, catching multiple tags

so I have this for example:

<message>
test
test 
test
</message>
<message>
test2
test2
test2
</message>

And now I want to catch both things between <message> and </message>

I have this regex: /<message>\n(.*)<\/message>\n/mi The result I get is this:

Match 1:

test
test 
test
</message>
<message>
test2
test2
test2

I want this to be the result:

Match 1:

test
test
test

Match 2:

test2
test2
test2

Is there a way to solve my problem? Thanks for every answer.

Upvotes: 0

Views: 50

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174706

You need to add a quantifier ? inorder to do a reluctant match(aka shortest possible match).

<message>\n(.*?)<\/message>

DEMO

Upvotes: 1

vks
vks

Reputation: 67968

<[^>]*>(.*?)<\/[^>]*>

You can use this with g and s flags.See demo.

http://regex101.com/r/nG1gU7/33

Upvotes: 1

Related Questions