Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expression for phone number

Status
Not open for further replies.

jimny

Technical User
Oct 18, 2002
52
0
0
US
I am trying to extract a phone number from a text string,
display the first part of string before the phone number, then the second part, then the phone number.
I am first splitting the the string:
Code:
[i]
text = cstr(rsAd("adtext"))
aryTxt = split(text," ")
[/i]
then am looping to find a match and capturing the array index:
Code:
[i]
Set regex = New regExp
regex.Pattern = "\d{3}-\d{3}-\d{4}"
for x=0 to ubound(aryTxt)
  if regex.Test(aryTxt(x)) then
    phn = x 
  end if
next
[/i]
then I am looping twice to rebuild the strings:
Code:
[i]
FrstStr = ""'build string prior to phone #
for y=0 to phn - 1
 FrstStr = Frststr & aryTxt(y) & " "
next
SecStr = ""'build string after phone #
for z=phn + 1 to ubound(arytxt)
  SecStr = SecStr & arytxt(z)& " "
next
[/i]
then am displaying:
Code:
[i]
Frststr & " " & secStr & " " & aryTxt(phn)
[/i]

It is ugly but it works except that I am not able to get the syntax for the regular expression to match the pattern 123-123-1234 or 123 123 1234(spaces)

I have tried several variatons of somethign like:
"\d{3}(-|\s)\d{3}(-|\s)\d{4}", but can't get it.

If someone could help with the regex it would be greatly appreciated and a great Kharma builder
 
would it be possible to just check for the area code numbers and then the last 4 numbers to make sure you have it all?

something like this:

regex.Pattern = "\d{3}*\d{4}"


basically make it more general, but make sure you are getting all of the number.

hope that helps.
 
try:

Code:
regex.Pattern = "\d{3}[- ]\d{3}[- ]\d{4}"

the [] mean you can match anything within them, so in this case exactly one of either a dash or a space. Your (-| ) should also work.

Your problem is that you are using aryTxt = split(text," "), which will split their phone number into 3 when they use spaces rather than dashes. So, it will never match with spaces because it has already been broken up.

Since you are using reg exps, you can extract the match from the string without using split() at all. It will be much easier. -> I'll have a look at the syntax to do it now

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
ok (in ASP):
Code:
   <%
    Dim RegX, Match, subMatch, matchesColl, sPattern, sReplaceWith, sString, strFinalOutput
    
    sPattern = &quot;^(.*?) (\d{3}[- ]\d{3}[- ]\d{4}) (.*?)$&quot;
    sString = &quot;start part 000 000 0000 end part&quot;

    Set RegX = NEW RegExp
    RegX.Pattern = sPattern
    RegX.Global = True
    RegX.IgnoreCase = True
    
    Set matchesColl = RegX.Execute(sString)
    
    Response.Write(&quot;Matches found: <br>&quot;)
    For Each Match in matchesColl
    	Response.Write(&quot; {&quot; & Match & &quot;} <br>&quot;)
    	Response.Write(&quot;Submatches: &quot;)
    	For Each subMatch in Match.subMatches
    	  Response.Write(&quot;[<b>&quot; & subMatch & &quot;</b>] &quot;)
    	Next
    Next
    
    strFinalOutput = matchesColl(0).subMatches(0) & &quot; &quot; & matchesColl(0).subMatches(2) & &quot; &quot; & matchesColl(0).subMatches(1)
    Response.Write(&quot;<br>&quot; & strFinalOutput)
    %>

which outputs:
Matches found:
{start part 000 000 0000 end part}
Submatches: [start part] [000 000 0000] [end part]
Final Output: start part end part 000 000 0000

The pattern matches a number and anything before and after it, and remembers them using ( ) notation. These can then be referred to later - see my code where I both loop through all matches & submatches and also specifically refer to particular ones to build the final output string.

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
clarkin,
thanks for the thorough and rapid response...

When implementing the above code,
When I loop through the
&quot;For each submatch in match.submatches&quot;
it displays the 3 submatches beautifully,
but when I access the object matchesColl(0) and its'
property submatches:

matchesColl(0).subMatches(0) & &quot; &quot;
& matchesColl(0).subMatches(2) & &quot; &quot;
& matchesColl(0).subMatches(1)

for some reason I am not displaying subMatches(1).
I have played around using different combos of match and submatch, but have not been able to debug, any insight?

And once again, thanks for that awesome response.


 
Never Mind...Figured it out.

now if I can only set it to match once...

Thanks again!!
 
now if I can only set it to match once...

regex.global = false



=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
 
I thought it would only match once even with global = true as it has both start ^ and end $ in the pattern.. what's your search string?



Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
clarkin,
My bad,
You are correct, it is only matching once. had a line that I thought I commented out for display but had not.

Once again thanks for the thorough response.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top