groovy - regex to retrieve inner html tag

I wanted to try to match the inner part of the string between the span tags where it is guaranteed that the id of this span tags starts with blk.

How can I match this with groovy?

Example :

<p>I wanted to try to <span id="blk1">match</span> the inner part of the string<span id="blk2"> between </span>the span tags <span>where</span> it is guaranteed that the id of this span tags <span id="blk3">starts</span> with blk.</p>

According to the example above,I want to have

   match
   between
   starts

I tried the following , but it returns null;

 def html='''<p>I wanted to try to <span id="blk1">match</span> the inner part of the string<span id="blk2"> between </span>the span tags <span>where</span> it is guaranteed that the id of this span tags <span id="blk3">starts</span> with blk.</p>''' 

 html=html.findAll(/<span id="blk(.)*">(.)*<\/span>/).join();
 println html;

Answers


Rather than messing around with Regular Expressions, why not just parse the HTML and then extract the nodes from it?

@Grab( 'net.sourceforge.nekohtml:nekohtml:1.9.18' )
import org.cyberneko.html.parsers.SAXParser

def html = '''<p>
             |  I wanted to try to <span id="blk1">match</span> the inner part
             |  of the string<span id="blk2"> between </span> the span tags <span>where</span>
             |  it is guaranteed that the id of this span tags <span id="blk3">starts</span>
             |  with blk.
             |</p>'''.stripMargin()

def content = new XmlSlurper( new SAXParser() ).parseText( html )

List<String> spans = content.'**'.findAll { it.name() == 'SPAN' && it.@id?.text()?.startsWith( 'blk' ) }*.text()

Need Your Help

PHP Help to simplify an IF statement when retrieving results from a function

php arrays function if-statement execution-time

Let's say i have this function that seeks for a value inside a bidimensional array:

Trying to execute PowerShell 2.0 script from a .NET installer's custom action

c# .net powershell

I've been working on this issue for a couple days and have read several posts here, but I can't get my implementation to work. I am calling the powershell script during the Commit custom action. Wh...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.