RegularExpression for finding word

  • Thread starter Thread starter Joe
  • Start date Start date
J

Joe

I have an expression where I need to replace words with values (#'s).

Take the following expression:

val1 *2/( val3 + val4*12/val1)

Given this example how could I replace val1 with the number 15?
Note: the spacing in the above expression was intentional. There may or may
not be spaces between the variables and the operators but there can NOT be
no space between a variable name and a literal value;
 
Joe said:
I have an expression where I need to replace words with values (#'s).

Take the following expression:

val1 *2/( val3 + val4*12/val1)

Given this example how could I replace val1 with the number 15?
Note: the spacing in the above expression was intentional. There may or
may
not be spaces between the variables and the operators but there can NOT be
no space between a variable name and a literal value;

Have you tried "\bval1\b"? That's supposed to match only "whole word"
val1's.

Niki
 
If you are doing a simple replace like this then using String.Replace would
be faster.

Dim formula as String = "val1 *2/( val3 + val4*12/val1)"
Dim value as Double = 15
formula = formula.Replace("val1", value.ToString())

Else you would use the pattern
val(\d+)
to match your value place holders. This puts the value index in group 1
(Match.Groups(1)) so you know what number to replace the value by.

Robby
 
Back
Top