Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ASP.NET/VB/REGEX: Leading Stuff  Zapped

Posted on 2013-05-20
5
Medium Priority
?
361 Views
Last Modified: 2013-06-03
This code does not work if there are special characters like colons or hyphens before the matching phone number.

Imports System.Web
Partial Class test
    Inherits System.Web.UI.Page
    Protected Function REGEX(text As String) As String
        Dim input As String = ""

input = 
"This works 423-555-1212" & vbCrlf &
"This works (423) 555-1212" & vbCrlf &
"This works 4235551212" & vbCrlf &
"" & vbCrlf &
"This looses the hyphen - 4235551212" & vbCrlf &
"This losses the space before the phone number: 423-555-1212" & vbCrlf &
"This losses the space before the phone number: (423) 555-1212" & vbCrlf &
"This losses the space before the phone number: 4235551212" & vbCrlf &
"(This losses the space before the phone number) 423-555-1212" & vbCrlf &
"This losses the space before the phone number; 423-555-1212"

' Phone Numbers
input = RegularExpressions.Regex.Replace(input, "(\s?)(?!<\d[^a-z\d\n]{0,2})1?[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)(?![a-z\d\n]{0,2}\d)", "$1$2$3$4.$5$6$7.$8$9$10$11")

Return input

    End Function
    Protected Sub _Index_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles me.load
        out.Text = REGEX(Request.Form("c"))
    End Sub
 
End Class

Open in new window

0
Comment
Question by:hankknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39181306
What is supposed to happen, or what should the data look like after the function is run?
0
 
LVL 16

Author Comment

by:hankknight
ID: 39181381
This:
This works 423-555-1212
This works (423) 555-1212
This works 4235551212
This looses the hyphen - 4235551212
This losses the space before the phone number: 423-555-1212
This losses the space before the phone number: (423) 555-1212
This losses the space before the phone number: 4235551212
(This losses the space before the phone number) 423-555-1212
This losses the space before the phone number; 423-555-1212

Open in new window

Becomes this:
This works 423.555.1212
This works 423.555.1212
This works 423.555.1212
This looses the hyphen 423.555.1212
This losses the space before the phone number423.555.1212
This losses the space before the phone number423.555.1212
This losses the space before the phone number423.555.1212
(This losses the space before the phone number423.555.1212
This losses the space before the phone number423.555.1212

Open in new window

But it should become this:
This works 423.555.1212
This works 423.555.1212
This works 423.555.1212
This looses the hyphen - 423.555.1212
This losses the space before the phone number: 423.555.1212
This losses the space before the phone number: 423.555.1212
This losses the space before the phone number: 423.555.1212
(This losses the space before the phone number) 423.555.1212
This losses the space before the phone number; 423.555.1212

Open in new window

Notice how certain chararacters including spaces, hyphen, and colons are lost if there is a special character before the phone number.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39181457
You pattern seems overly-complicated. Can we try with this instead:

...

' Phone Numbers
input = RegularExpressions.Regex.Replace(input, "\b(?:(?<=\d{3})-(?=\d{4})|(?<=\d{3})-(?=\d{3}))\b", ".")  ' Replace delimited phone numbers
input = RegularExpressions.Regex.Replace(input, "\b(\d{3})(\d{3})(\d{4})\b", "$1.$2.$3")  ' Replace non-delimited phone numbers

...

Open in new window

0
 
LVL 16

Author Comment

by:hankknight
ID: 39181515
Your pattern does not work with this:
Phone Number: (423) 555-1212

Open in new window

It should become:
Phone Number: 423.555.1212

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 39181963
Add one more line after the other two:

input = RegularExpressions.Regex.Replace(input, "\((\d{3})\)\s*(?=\d{3})", "$1.")  ' Replace parenthesized area codes

Open in new window

0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question