Got more questions? Find advice on: ASP | SQL | XML | Windows
Welcome to RegexAdvice Sign in | Join | Help

VB.NET RegEx.Replace replacing the matched characters + 1

  •  03-06-2008, 12:00 PM

    VB.NET RegEx.Replace replacing the matched characters + 1

    I have the follwing code in VB.NET:

     Dim strPhone As String = oRow("phone").ToString.ToLower  ' data is like 123-555-0000X81455


     If Regex.IsMatch(strPhone.ToLower, "ext.|ext|ex.|ex|x.|x") Then

      strPhone = Regex.Replace(strPhone.ToLower, "ext.|ext|ex.|ex|x.|x", "A")

     End If

    It returns this 123-555-0000A1455.  It is replacing not only the match, but the character following the match.  I am transforming data from a legacy system where the entire phone number and extension is stored in 1 field and I need to break the extension out from the phone number, so having the replace removing the first digit of the extension is unacceptable.  I had implemented it differently where when I find the match using Regex I create an array of the various methods of entering the extension (the RegEx Match expression) like this:


     Dim strExtension As String = String.Empty

     Dim arrFindStr As String() = New String() {"ext.", "ext", "ex.", "ex", "x.", "x"}

     Dim intCounter As Integer = 0

     Dim intLocation As Integer = -1


     While intCounter < arrFindStr.Length And intLocation = -1

      intLocation = Phone.IndexOf(arrFindStr(intCounter))

      intCounter += 1

     End While


     strExtension = Phone.Substring(intLocation + arrFindStr(intCounter - 1).Length)


    This seems like a waste if I can do the Replace and then just use the replacement character.  Then is it is done in this code:

     strExtension = Phone.Substring(Phone.IndexOf("A") + 1)


    I am new to using Regular Expressions.  I come from a SQL Server background and there is not Regular Expression Support in T-SQL. 


    Jack Corbett
    New Tribes Mission (www.ntm.org)
    Software Developer, O.D.A.A.
    Filed under: ,
View Complete Thread