Regex help

  • Thread starter Thread starter Frank Rizzo
  • Start date Start date
F

Frank Rizzo

Hello, i am regex newbie. I want to extract text between 2 markers. I
want all the text that happens to be between <h2> and </h2>.

So far I got this:
<h2>.*</h2>

for this sample input:
<h2>hello</h2>
<h2>goodbye</h2>
<h2>wait</h2>

However, the regex also returns the markers, while I only want hello,
goodbye, wait.

I know I can Substring it, but I want to do it with regex alone.

Thanks.
 
Miroslav said:
I suppose that you have a problem with regex greediness. To kill the
greediness and turn on the laziness use:

<h2>.*?</h2>

You are the man. That was my next question, how to turn lazyness on (i
actually didn't know that it was called lazyness).

Thanks.
 
Hi,

To capture the matched text inside the markers, you need to group it with
"()":

<h2>(.*?)</h2>


After you got the Match object, use its .Groups[1] to reference the first
group, use its .Value property to read the matched text.

You can also use named group, for example:

<h2>(?<the_header>.*?)</h2>


Then you can use the group name to reference it: .Groups["the_header"]


Hope this helps.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi,

Please reply here to let us know if this post can be closed or not. Thanks.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Walter said:
Hi,

Please reply here to let us know if this post can be closed or not. Thanks.

Walter,

For whatever strange reason, your example did not work, even though it
made sense. However, this pattern worked fine:

(?<=<h2>).*?(?=</h2>)

The post can be closed.

Regards and thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top