AllisonC
AllisonC

Reputation: 3100

Why is this regular expression matching so much?

I am trying to use http://www.regexr.com/ to create a regular expression.

Basically I am looking to replace something that matches <Openings>any other tags/text</Openings>

<Openings><opening><item><x>3</x><y>3</y><width>10.5</width><height>13.5</height><type>rectangle</type><clipX>0</clipX><clipY>0</clipY><imgsrc></imgsrc></item></opening></Openings> 

I started with ([\<Openings\>])\w+ (http://regexr.com/393mv ) but it seems to be matching too many things. Right now that regular expression should only match <Openings>.

Upvotes: 1

Views: 36

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174766

Regex to match the whole Openings tag is,

<Openings>.*?<\/Openings>

If you want to capture the contents inside the Openings tag then try the below,

<Openings>(.*?)<\/Openings>

Upvotes: 1

Joe
Joe

Reputation: 63424

([\<Openings\>])\w+

The brackets mean "Match any character in this". You should use

(\<Openings\>)\w+

which matches specifically "<Openings>" plus one or more word characters.

Upvotes: 0

Related Questions