Regex help

G

Guest

I'm new at using Regex, and I'm using VB.NET.
I can't seem to get this to work. I'm replacing matched items in a text file.
The format is v (upper or lower) and then 4-5 integers.
I'm trying (many variations of):

'Replace version
newText = Regex.Replace(contents, "(\bv)([0-9]{4,5})", VersionNew,
RegexOptions.Multiline)

where; newVersion is just a string, and contents is the stream of the text
I can get this to work if I 'hard code' the item to replace in the file, but
I want to use regex since this might change.

I've been using 'http://regexlib.com/RETester.aspx' to help build the
expression.
All the examples are in C#, and whne I convert the characters '@' is not
recongized?

Help?
 
G

Guest

Here is a sample from some work I just did.

string pattern = @"( xmlns=\"")(.*)(\"")";
_logFileText = Regex.Replace(_logFileText, pattern, string.Empty);

I am not quite sure what you are referring to regarding the "@", but here is
an example of what I believe is called string literal. Sometimes you have to
use a string literal to get the escape characters to work properly.
 
F

Flinky Wisty Pomm

If I'm not mistaken, the parentheses set up a match group - you want to
match a V01234, but you're actually matching the V and the digits
separately. Have you tried "v\d{4,5}" ? Don't forget to use
RegexOptions.IgnoreCase.

Are you intending to match the two parts separately?

The @ tells the C# compiler to treat everything inside the quotes as
literal text, otherwise in the example above, it would first try to
convert \d to an escaped character and choke horribly.
 
G

Guest

Well, I've tried converting C# examples to vb.net but maybe I'm missing
something.
How do I do the same in VB.net? I can't use '@'.
And no, I really just want to test the entire text and replace matches for
v(orV) and 4-5 integers.
Ideas?
Thanx, for everyone's reply's.
 
F

Flinky Wisty Pomm

The @ is only there because the \ character is the escape char in C#' -
VB.Net uses... the ritual sacrifice of a goat or something... so you
can drop the @ from the beginning of the string.
 
F

Flinky Wisty Pomm

Actually, that might be a lie - if VB.Net complains about invalid
escape sequences after you drop your @ symbol, replace the \ with \\
 

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