Help with regular expresion

  • Thread starter Thread starter LEM
  • Start date Start date
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!
 
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.
 
So do you suggest to go character by character until I find my pattern?
The text buffer can be very big.
 
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>
 
LEM,

Are you trying to get multiple instances of this pattern from one very
long string, or are you getting it string by string?
 
<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
 
Back
Top