1

I want to match any block of text starting by this

<eu:img

followed by anything but

> 

followed by

"<bean:message key="

followed by anything even a

>

For instance I want this text to be matched from eu:img to message key="

<eu:img src="static/image/icons/erase12.png"
title="<bean:message key="attachedFile.delete.tooltip"/>"></eu:img>

But not this text :

<eu:img src="static/image/icons/erase12.png" title="someTitle"></eu:img>
<link title="<bean:message key="attachedFile.delete.tooltip"/>"></link>

Here is what I tried :

(?s)^<eu:img((?!.*?>).*.{1}?(\=\"<bean:message key\=\"))

Regular expression visualization

Debuggex Demo

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
Pdv
  • 147
  • 10
  • Are you looking to capture anything after ``, but then: "For instance I want this text to be matched from `eu:img` to `message key=`" – fugu Dec 05 '13 at 17:12
  • 2
    **Don't use regular expressions to parse XML. Use a proper XML parsing module.** You cannot reliably parse XML with regular expressions, and you will face sorrow and frustration down the road. As soon as the XML changes from your expectations, your code will be broken. Start [here](http://stackoverflow.com/questions/487213/whats-the-best-xml-parser-for-perl) for an SO question about which module to use, but also note that there are likely better more current lists on the Web. – Andy Lester Dec 05 '13 at 17:42

1 Answers1

3

Try this:

/(<eu:img[^>]+<bean:message key=)/gs

OP's EDIT : Good answer but not the right syntax for Regex engine I use : Eclipse (Perl 5 I think). This works fine anyway :

(?s)<eu:img[^>]+="<bean:message key="
  • <eu:img matches the characters <eu:img literally (case sensitive)
  • [^>]+ match a single character not present in the list (>) Quantifier: Between one and unlimited times, as many times as possible, giving back as needed [greedy]
  • <bean:message key= matches the characters <bean:message key= literally (case sensitive)
fugu
  • 6,417
  • 5
  • 40
  • 75