regular expression help

H

Howard

I need to write a regex that will match everything in between this tag

<div width="137">(.*)</duv>

it works when (.*) is a single line but i couldn't get it to work if the
opening and closing tags are not on the same line

example
<div width="137">hello
world
</duv>

it should match
hello
world

Thanks in advance,
Howard
 
A

AlanT

By default the . does not match a newline.

You can set the SingleLine option when creating the Regex

e.g.

dim r as new Regex("<div width="137">(.*)</duv>",
RegexOptions.SingleLine)

or modify the pattern to set the mode to singleline

dim r as new Regex("<div width="137">(?s)(.*)</duv>")


NOTE: This will include the newline in your capture group so you may
want to remove it before displaying

e.g.
matchedText = matchedText.Replace(System.Environment.NewLine, "")


hth,
Alan.
 
A

AlanT

Whoops, wrong group. lets try

Regex r = new Regex("<div width="137">(.*)</duv>",
RegexOptions.SingleLine)
Regex r = new Regex("<div width="137">(?s)(.*)</duv>")

Alan.
 
C

Cerebrus

Hi Alan,

I think you forgot to escape the quotes. Happens frequently with us
VB-lovers... ;-))

Regex r = new Regex(@"<div width=""137"">\(.*\)</duv>",
RegexOptions.Singleline);
Regex r = new Regex(@"<div width=""137"">(?s)(.*)</duv>");

Regards,

Cerebrus.
 

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

Top