Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 291
  • Last Modified:

Need help with a couple of regex and replacement string

1.      DisplayName:  I want to add a space after the comma for example: Doe,john  This should be Doe, John
2.      Telephone:  I want it formatted as “(323) 442-2455”, not as “+1 323 442 2455”

I need help with the regex for the above 2 and then the Replacement string for Regex.Replace method.  Thank you.

0
lapucca
Asked:
lapucca
  • 8
  • 5
1 Solution
 
Terry WoodsIT GuruCommented:
1.
Find pattern:
,(?!\s)
and replace with
,
The comma on the previous line has a space after it. Note that the negative lookahead (?!\s) prevents a space being added if the comma already has a space after it.
0
 
Terry WoodsIT GuruCommented:
2.
Find pattern "^\+\d\s?(\d{3})\s?(\d{3})\s?(\d{4})"
And replace with:
($1) $2-$3
0
 
Terry WoodsIT GuruCommented:
I think this is the C# syntax:
  myResult = Regex.Replace(myString,@",(?!\s)",", ");
  myResult = Regex.Replace(myString,@"^\+\d\s?(\d{3})\s?(\d{3})\s?(\d{4})","($1) $2-$3");
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
lapuccaAuthor Commented:
So, using http://msdn.microsoft.com/en-us/library/e7f5w83z.aspx 
I'm not sure what to put for the replacement string for the Regex.Replace method though.  I mean what to put there so it would keep all the name's text and also adding that needed space after the comman?  Thanks.
0
 
lapuccaAuthor Commented:
Terry, where can I find information on what is this $1, $2, $3.....  Is this grouping in regex?
0
 
Terry WoodsIT GuruCommented:
Yes, it refers back to the 1st, 2nd and 3rd groups defined by the round brackets in the pattern.
0
 
lapuccaAuthor Commented:
Ok, I can see that for the phone number.  However, I'm still not able to see that for the display name where I want to add a space where there isn't one.  The replacement string only has ", ".  That doesn't seem right, does it?
0
 
Terry WoodsIT GuruCommented:
Yes - you're replacing a [comma without a space after it] with [a comma with a space after it]
0
 
lapuccaAuthor Commented:
What about the rest of the Displayname's text?  Are you saying it only replaces the comman? so if i have this :
Mystring = "Doe,John";
Regext.Relace(Mystring, ",@",(?!\s)",", ");
now Mystring would contian "Doe, John"?
0
 
Terry WoodsIT GuruCommented:
Yes, that's right.
0
 
Terry WoodsIT GuruCommented:
Except there's an error in your code. It should be:
Regext.Relace(Mystring, @",(?!\s)",", ");
0
 
lapuccaAuthor Commented:
Thank you.
0
 
Terry WoodsIT GuruCommented:
Correction!
Regex.Replace(Mystring, @",(?!\s)",", ");
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 8
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now