regex problem

  • Thread starter Thread starter bob lambert
  • Start date Start date
B

bob lambert

Hi,

Here is another problem I am having, after discovering
regular expressions are available in vb.net std 2002.

I have a line read from file that produces the following
string:

inputln = "1.0 kt 1.6878099 ft/sec"

I tried this (below) and it does not work, and I cannot
figure out why (excuse the clunkiness please)

trying to get

units_typ_io_count(1, 1, count_speed)=1.0

code snippet

Dim units_typ_io_count(4, 2, 0) As Double
Dim riou As New Regex("?<numrtr>^\d+\.\d+([eE][+ -]\d+)?
[^ ].*?<dentr>\d+\.\d+([eE][+ -]\d+)?[ ]+?<outunits>
[^ ].+$", RegexOptions.Compiled)
..
..
..
ReDim Preserve units_typ_io_count(4, 2, 1)
units_typ_io_count(1, 1, 1) = CDbl(riou.Match
(inputln).Result("${numrtr}"))



Thanks for the help

Bob Lambert
 
Bob,

I tried the following regular expression:

(?<numrtr>^\d+\.\d+([eE][+ -]\d+)?).*(?<dentr>\d+\.\d+([eE][+ -]\d+)?)+(?<ou
tunits>.+$)

using this test string: 1.0e-10 kt 1.6878099e+6 ft/sec

it returned the following named groups:
numrtr: 1.0e-10
dentr: 1.6878099e+6
outunits: ft/sec

Is this close to what you were trying to do? If so, note that I had to make
a few changes to your regular expression to accomplish it.

I tested this using Expresso, which is available at:

http://www.ultrapico.com

Jim
 
Jim,

Thanks for the feedback. I finally got it to work by
removing the <numrtr> and other parameters. Instead, I
enclosed the parts I wanted in parentheses, and referred
to them in the .result as below.

Not sure why the <name> deal would not work.

Bob

input string:
1.0 kt 1.6878099 ft/sec

code:
Dim riou As New Regex("^(\d+\.\d+([eE][+ -]\d+)?) [^ ].*
(\d+\.\d+([eE][+ -]\d+)?)[ ]+?([^ ].+)$", _
RegexOptions.Compiled)
..
..
..
units_typ_io_count(1, 1, 1) = CDbl(riou.Match
(inputln).Result("${1}"))
units_typ_io_count(1, 2, 2) = CDbl(riou.Match
(inputln).Result("${3}"))
units_desc_typ_count(1, 1) = riou.Match(inputln).Result
("${5}")

output:
units_typ_io_count(1, 1, 1) = 1.0
units_typ_io_count(1, 2, 2) = 1.6878099
units_desc_typ_count(1, 1) = "ft/sec"
 
Your original expression did not enclose the named capture within
parenthesis. You need to use the following syntax:

(?<name>matchsting)

rather than:

?<name>matchstring

Jim
 
Back
Top