RegEx c# help

N

natebruneau

I wrote this regex

<font>.*\((?<Inside>.*?)\)</font>

"<font> (Hi there)</font></font><font> (Hi there)</font><font> (Hi
there)</font><font> (Hi there)</font><font> (Hi there)</font>"

However it only returns the whole thing
<font> (Hi there)</font></font><font> (Hi there)</font><font> (Hi
there)</font><font> (Hi there)</font><font> (Hi there)</font> as a
Single match

Any ideas on how I can do a non-greedy match ? Thanks!

Nate
 
J

jehugaleahsa

I wrote this regex

<font>.*\((?<Inside>.*?)\)</font>

"<font> (Hi there)</font></font><font> (Hi there)</font><font> (Hi
there)</font><font> (Hi there)</font><font> (Hi there)</font>"

However it only returns the whole thing
<font> (Hi there)</font></font><font> (Hi there)</font><font> (Hi
there)</font><font> (Hi there)</font><font> (Hi there)</font> as a
Single match

Any ideas on how I can do a non-greedy match ? Thanks!

Nate

Say I was looking for the shortest "quoted string".

99% of the time I write something like this to find the smallest
match:

Regex regex = new Regex("\"([^\"]*)\"");

This will find a quote, and pull until it sees another quote, then the
quote. This can be extended to something more like you are looking
for.

Regex regex = new Regex(".*\((?<Inside>[^\)]?)\)");

You see, it will look until it finds a ). You just need to make sure
that you don't have any ) inside.
 
R

Rad [Visual C# MVP]

I wrote this regex

<font>.*\((?<Inside>.*?)\)</font>

"<font> (Hi there)</font></font><font> (Hi there)</font><font> (Hi
there)</font><font> (Hi there)</font><font> (Hi there)</font>"

However it only returns the whole thing
<font> (Hi there)</font></font><font> (Hi there)</font><font> (Hi
there)</font><font> (Hi there)</font><font> (Hi there)</font> as a
Single match

Any ideas on how I can do a non-greedy match ? Thanks!

Nate

I'm assuming you want to extract the text (Hi There) from the font
tags. In which case try this regex:

<font>(?<Inside>.*?)</font>
 

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