Jump to content

Recommended Posts

Posted

Code for formatting a Social Security Number! (Thanks again Valik):

Local $sOriginal = "123121234"
Local $sFormatted = StringRegExpReplace($sOriginal, "(.{3}){1}(..{1})", "\1-\2-\3")
MsgBox(0, "", $sFormatted)

We have enough youth. How about a fountain of SMART?

Posted

Code for formatting a Social Security Number! (Thanks again Valik):

What a concept! Learning by example (extrapolation)... Let's hope these examples are added to the next help file?
...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
  • 4 years later...
Posted

Sorry to reopen a closed conversation but I did a search to see if I could find some code to format a phone number and I was presented with this conversation. I tried to implement Jefhal's code to format a 10 digit phone number but it only displays "456-7890-" and not "123-456-7890". I can't figure out how to get it to display correctly. I have tried entering a zero in the second line but it only produces the whole phone number followed by a dash then the last four followed by a dash. Here is what I tried

Local $sFormatted = StringRegExpReplace($sOriginal, "(.{3}){2}(....{1})", "\0-\2-\3")
Can someone help me figure this out? I have read the help file but it doesn't make sense to me. It would be helpful to have an example that I can play with and learn from.

Posted

Sorry to reopen a closed conversation but I did a search to see if I could find some code to format a phone number and I was presented with this conversation. I tried to implement Jefhal's code to format a 10 digit phone number but it only displays "456-7890-" and not "123-456-7890". I can't figure out how to get it to display correctly. I have tried entering a zero in the second line but it only produces the whole phone number followed by a dash then the last four followed by a dash. Here is what I tried

Local $sFormatted = StringRegExpReplace($sOriginal, "(.{3}){2}(....{1})", "\0-\2-\3")
Can someone help me figure this out? I have read the help file but it doesn't make sense to me. It would be helpful to have an example that I can play with and learn from.

I think I figured it out. This works for formatting a phone number.

Local $sOriginal = "1234567890"
Local $sFormatted = StringRegExpReplace($sOriginal, "(.{3}){1}(...{1})", "\1-\2-\3")
MsgBox(0, "", $sFormatted)
Posted (edited)

I think I figured it out. This works for formatting a phone number.

Local $sOriginal = "1234567890"
Local $sFormatted = StringRegExpReplace($sOriginal, "(.{3}){1}(...{1})", "\1-\2-\3")
MsgBox(0, "", $sFormatted)

Either of these seems sufficient:

Local $sFormatted = StringRegExpReplace($sOriginal, "(...)(...)", "\1-\2-")
Local $sFormatted = StringRegExpReplace($sOriginal, "(.{3})(.{3})", "\1-\2-")

I like this for the original OP's request (Mac address):

Local $sFormatted = StringRegExpReplace($sOriginal, "(..)(?!$)", "\0-")

Edit: Ok, I've just about totally confused myself with regular expressions (again), so am not sure this is the most direct way, but this works with 7 or 10-digit phone numbers:

Local $sFormatted = StringRegExpReplace($sOriginal, "(...)?((....)$|(...))", "\1-\2")
Edited by Spiff59

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...