Results 1 to 12 of 12

Thread: RegExp

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    565

    RegExp

    I am getting an error. I am attempting to convert a phone number with () and - to the 10 digit format. Also, I would like 7 digit numbers converted to 10 also, but I think I can handle that once I get this going. Anyways, let me know if you have any tips. Thanks!

    Error:
    Code:
    Error	1	Overload resolution failed because no accessible 'New' accepts this number of arguments.	C:\Documents and Settings\User\My Documents\Visual Studio 2008\Projects\JeffComputersPOS1\JeffComputersPOS1\frmPreTicket.vb	34	14	JeffComputersPOS1
    I got the example from:
    http://www.4guysfromrolla.com/webtech/031302-1.shtml

    vb Code:
    1. Function get10DigitPhoneNumber(ByVal strUnformattedPN As String) As String
    2.         'Create a regular expression object
    3.         Dim re
    4.         re = New System.Text.RegularExpressions.Regex 'RegExp was the example, but it did not work either
    5.  
    6.         'System.Text.RegularExpressions
    7.  
    8.         'Specify the pattern
    9.         re.Pattern = "(\d{3})(\d{3})(\d{4})"
    10.  
    11.         'Use the replace method to perform the formatting
    12.         Dim strFormattedPN
    13.         Return re.Replace(strUnformattedPN, "($1) $2-$3")
    14.  
    15.     End Function
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  2. #2
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: RegExp

    The error is telling you exactly what is wrong. The Regex class has two accesible constructors:

    Regex(pattern as String)
    Regex(pattern as String, options as RegexOptions)

    The compiler is telling you that you are attempting to use a constructor that doesn't exist. Notice that IntelliSense shows you the overloaded methods after you type the first "(" after Regex. You'll see these two options there.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    565

    Re: RegExp

    This does not seem to be working. Maybe I should change the expression? I just want the 7 or 10 digits returned. I want the - and () removed.

    Example phone numbers:
    (314) 333-4444
    314-333-44444

    vb Code:
    1. Function get10DigitPhoneNumber(ByVal strUnformattedPN As String) As String
    2.         'Create a regular expression object
    3.         Dim re As New System.Text.RegularExpressions.Regex("(\d{3})(\d{3})(\d{4})")
    4.         're = New System.Text.RegularExpressions.Regex("(\d{3})(\d{3})(\d{4})") 'RegExp was the example, but it did not work either
    5.  
    6.         'System.Text.RegularExpressions
    7.  
    8.         'Specify the pattern
    9.         're.Pattern = "(\d{3})(\d{3})(\d{4})"
    10.  
    11.         'Use the replace method to perform the formatting
    12.         'Dim strFormattedPN
    13.         Return re.Replace(strUnformattedPN, "($1) $2-$3")
    14.  
    15.     End Function
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: RegExp

    do you just want to reformat phone numbers?

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: RegExp

    try this:

    vb Code:
    1. Dim testStr As String = "(314) 333-4444"
    2. testStr = Regex.Replace(testStr, "\(|\)", "")
    3. testStr = Regex.Replace(testStr, " ", "-")
    4. MsgBox(testStr)

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    565

    Re: RegExp

    Yes.

    Quote Originally Posted by .paul.
    do you just want to reformat phone numbers?
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    565

    Re: RegExp

    I also want to just take the 1st 7 or 10 digits based on what they provided. That is why I was thinking a Regex could be helpful, or at least good practice

    Quote Originally Posted by .paul.
    try this:

    vb Code:
    1. Dim testStr As String = "(314) 333-4444"
    2. testStr = Regex.Replace(testStr, "\(|\)", "")
    3. testStr = Regex.Replace(testStr, " ", "-")
    4. MsgBox(testStr)
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: RegExp

    are you trying to take the numbers from a text file, and then reformat them?
    post your text file.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    565

    Re: RegExp

    No, users are entering the phone numbers into a web form and I want to make sure they get formatted nice into the database. Is there an easy way to apply an data entry mask on a ASP page like with VB Forms? Thanks!

    Quote Originally Posted by .paul.
    are you trying to take the numbers from a text file, and then reformat them?
    post your text file.
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  10. #10
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: RegExp

    Quote Originally Posted by rex64
    No, users are entering the phone numbers into a web form and I want to make sure they get formatted nice into the database. Is there an easy way to apply an data entry mask on a ASP page like with VB Forms? Thanks!
    You can use a TextBox and a RegularExpressionValidator. The Validator has "U.S. Phone Number" as one of its standard expressions.

    http://msdn.microsoft.com/en-us/libr...validator.aspx

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    565

    Re: RegExp

    Yes, but I want the user to be able to type their number easily. I was hoping they could type it and Microsoft would add the () and - automatically, and then just give me the digits when I need them. Is this possible?

    Quote Originally Posted by nmadd
    You can use a TextBox and a RegularExpressionValidator. The Validator has "U.S. Phone Number" as one of its standard expressions.

    http://msdn.microsoft.com/en-us/libr...validator.aspx
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: RegExp

    try this. it'll accept the numbers in any of the formats you mentioned + return just the numbers

    vb Code:
    1. Dim testStr As String = TextBox1.Text
    2. Dim rx As New Regex("^(\(\d{3}\)|\d{3})?( |-)?\(?\d{3}\)?( |-)?\(?\d{4}\)?$")
    3. MsgBox(Regex.Replace(rx.Match(testStr).Value, "\(|\)| |-", ""))
    Last edited by .paul.; Aug 26th, 2008 at 01:36 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width