Solved

Need help with a couple of regex and replacement string

Posted on 2011-09-20
13
282 Views
Last Modified: 2012-08-13
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
Comment
Question by:lapucca
  • 8
  • 5
13 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 36571341
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
 
LVL 35

Expert Comment

by:Terry Woods
ID: 36571350
2.
Find pattern "^\+\d\s?(\d{3})\s?(\d{3})\s?(\d{4})"
And replace with:
($1) $2-$3
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 36571356
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
 

Author Comment

by:lapucca
ID: 36571358
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
 

Author Comment

by:lapucca
ID: 36571367
Terry, where can I find information on what is this $1, $2, $3.....  Is this grouping in regex?
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 36571371
Yes, it refers back to the 1st, 2nd and 3rd groups defined by the round brackets in the pattern.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:lapucca
ID: 36571392
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
 
LVL 35

Expert Comment

by:Terry Woods
ID: 36571397
Yes - you're replacing a [comma without a space after it] with [a comma with a space after it]
0
 

Author Comment

by:lapucca
ID: 36571416
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
 
LVL 35

Expert Comment

by:Terry Woods
ID: 36571419
Yes, that's right.
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 36571420
Except there's an error in your code. It should be:
Regext.Relace(Mystring, @",(?!\s)",", ");
0
 

Author Closing Comment

by:lapucca
ID: 36571421
Thank you.
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 36571423
Correction!
Regex.Replace(Mystring, @",(?!\s)",", ");
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
optimize  c# code 7 49
Expando 4 36
Windows Service to Receive TCP Packets 4 50
Image(2) 3 29
Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now