473,385 Members | 1,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Regular Expression Question. Please help!

I am trying to clean up a city name. Inside the letters only one of 3
characters (dot, space and hyphen) is allowed (1 max). For example:
Los-Angeles,Los Angeles and N.Westminster are ok.
Outside the letters nothing allowed.

So I need to do a replace and get this:
los angeles

from this:
___..&* los - .an#$geles. ^&...____ .
or this:
los-angeles

from this:
___..&* los- .an#$geles. ^&...____ .
Please help.

Nov 17 '05 #1
5 1185
HI

Try Regex ( Regular expressions)
http://www.c-sharpcorner.com/3/RegExpPSD.asp
http://www.codeguru.com/Csharp/.NET/...cle.php/c9325/

Shivprasad Koirala
C# , VB.NET , SQL SERVER , ASP.NET Interview Questions
http://www.geocities.com/dotnetinterviews/

Nov 17 '05 #2
Hi,

al*****@hotmail.com wrote:
I am trying to clean up a city name. Inside the letters only one of 3
characters (dot, space and hyphen) is allowed (1 max). For example:
Los-Angeles,Los Angeles and N.Westminster are ok.
Outside the letters nothing allowed.


Not what you asked and potentially n/a for what you are doing -- I know. :P

If you are trying to clean up postal addresses, consider purchasing an
inexpensive zipcode database for lookup. Between towns with odd names and
people molesting them at the time of entry, you will never get it right with
inflexible rules.

--
Chris Priede (pr****@panix.com)
Nov 17 '05 #3
This is what you are looking for :
^[^a-zA-Z]*|[^a-zA-Z]*$|[^a-zA-Z-. ]*([-. ])?[^a-zA-Z]*

myStringCleaned=Regex.Replace(myStringToClean,@"^[^a-zA-Z]*|[^a-zA-Z]*$|[^a-
zA-Z-. ]*([-. ])?[^a-zA-Z]*","$1");

Here some explanations :
^[a-zA-Z]* means that all line starting with anything else than a letter
must be replaced by $+ wich is empy (there is no capuring group).
| means "else"
[^a-zA-Z]*$ means that all line ending with anything else than a letter
must be replaced by $+ wich is empty too.
| means "else"
[^a-zA-Z-. ]* means : every sequence that does not match a letter or dot,
hyphen and space
([-. ])? means that you want to match one of these character
(dot, hyphen and space) the first time they appears and all the sequence
matched will be replaced by $+ wich is this characted you have just matched
[^a-zA-Z]* means anything else than a letter

$+ means you replace all sequences by the first match. For
the first and the second part, it is empty. For the last part, it could be
empty if there are no dot,hypen and space) or contains the last captured
group if there was a match.

So,

___..&* los - .an#$geles. ^&...____ .
will be replaced
by los angeles
and
___..&* los- .an#$geles. ^&...____ .

will be replaced
by los-angeles

To understand, let take the example ___..&* los- .an#$geles. ^&...____ .
___..&* will be matched in the first part of the regex and will be replaced
by nothing ($+=empty).
- . will be matched in the third part and will be replaced by -
($+=hyphen)
#$ will be matched in the third part and will be replaced by
nothing ($+=empty)
.. ^&...____ . will be matched in the second part of the regex and will be
replaced by nothing ($+=empty).
So you have 'empty' and 'los' and 'hyphen' and 'an' and 'geles' and 'empty'
that is 'los-angeles'.

Hope it helps,

Ludovic SOEUR
<al*****@hotmail.com> a écrit dans le message de
news:11**********************@o13g2000cwo.googlegr oups.com...
I am trying to clean up a city name. Inside the letters only one of 3
characters (dot, space and hyphen) is allowed (1 max). For example:
Los-Angeles,Los Angeles and N.Westminster are ok.
Outside the letters nothing allowed.

So I need to do a replace and get this:
los angeles

from this:
___..&* los - .an#$geles. ^&...____ .
or this:
los-angeles

from this:
___..&* los- .an#$geles. ^&...____ .
Please help.

Nov 17 '05 #4
al*****@hotmail.com wrote:
I am trying to clean up a city name. Inside the letters only one of 3
characters (dot, space and hyphen) is allowed (1 max). For example:
Los-Angeles,Los Angeles and N.Westminster are ok.
Outside the letters nothing allowed.


Others have already replied to your question. I was just curious about
this: what am I gonna do if I happen to live in
Krung-thep-maha-nakorn-boworn-ratana-kosin-mahintar-ayudhya-amaha-dilok-pop-nopa-ratana-rajthani-burirom-udom-rajniwes-mahasat-arn-amorn-pimarn-
avatar-satit- sakattiya-visanukam in Thailand, or even just in St John's
Chapel, Newcastle upon Tyne or Stoke-on-Trent?

Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 17 '05 #5
Thank you everyone for the brilliant ideas :-) especially to Ludovic!

to Oliver: I will try to go easy on "-" and "'" :-)

Thanks again!

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bradley Plett | last post by:
I'm hopeless at regular expressions (I just don't use them often enough to gain/maintain knowledge), but I need one now and am looking for help. I need to parse through a document to find a URL,...
18
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
3
by: Zach | last post by:
Hello, Please forgive if this is not the most appropriate newsgroup for this question. Unfortunately I didn't find a newsgroup specific to regular expressions. I have the following regular...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.