Help with regular expresion

L

LEM

Hi,

I'm trying to use Regex to find a string inside a text buffer.
I am trying to find this pattern:

<B>1.33594 VAL</B>

where 1.33594 is a number that may change. My problem is to find a
regular expression that can read that and extracts the number.

I have tried with the following but I don't get any matches back

string pattern = @"(<B>?<MyValue> USD</B>)";

Any ideas?

Thanks!
 
N

Nicholas Paldino [.NET/C# MVP]

LEM,

Why use a regular expression for this? You know that you will have the
string wrapped in <B> and </B>, you also know there will be a space, along
with up to three characters after it. Everything else is a number. It
would be very easy to code this up without regular expressions.

Hope this helps.
 
L

LEM

So do you suggest to go character by character until I find my pattern?
The text buffer can be very big.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

LEM said:
Hi,

I'm trying to use Regex to find a string inside a text buffer.
I am trying to find this pattern:

<B>1.33594 VAL</B>

where 1.33594 is a number that may change. My problem is to find a
regular expression that can read that and extracts the number.

I have tried with the following but I don't get any matches back

string pattern = @"(<B>?<MyValue> USD</B>)";

Any ideas?

Thanks!

The ?<name> part is part of the (?<name>...) construction, and can't be
placed anywhere but directly after the starting parenthesis. It just
specifies the name of the group, and doesn't consume any characters in
the match, so you also have to specify a pattern for the digits.

Try this:

<B>(<?<MyValue>\d+.?\d*) USD</B>
 
N

Nicholas Paldino [.NET/C# MVP]

LEM,

Are you trying to get multiple instances of this pattern from one very
long string, or are you getting it string by string?
 
K

Kevin Spencer

<B>(?<value>[\d.]+)\s\w{3}</B>

Assuming that you want to capture only the number into a named capturing
group with name "value".

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
L

LEM

Thanks, Göran. I got it to work making a few modifications to
your example.

Regards
 

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