Discussion:
Regex.Replace...
Gary Harpin
2007-10-12 10:23:40 UTC
Permalink
Efran Cobisi
2007-10-12 10:27:54 UTC
Permalink
Hi Gary,

You have to edit your expression in order to do exactly what you are
looking for; after replacing any ones and zeroes as you have done, you
could easily perform a regex replace as the following one:

// ...
cleankey = Regex.Replace(cleankey, "[^a-zA-Z2-7]", String.Empty);
// ...

HTH

--
Efran Cobisi
http://www.cobisi.com

Gary Harpin wrote:
> Hello all,
>
> I am trying to use Regex.Replace() to clean a string before working on
> it.
>
> The incoming string can be of the form " 5E1Y - UMCF - PZNG - RPR4 -
> H4KF - 6ICJ - HQ " or "5E1Y:UMCF:PZNG" or ... and I need to replace any
> ones and zeros with Is and Os and the strip it of everything that is not
> A-Z or 2-7. "5EIYUMCFPZNGRPR4H4KF6ICJHQ"
>
> So far I do the following:
> ...
> cleankey = cleankey.Replace("1", "I").Replace("O", "0");
> cleankey = Regex.Replace(cleankey, @"-([A-Z2-7])*", "");
> ...
>
> The data will come from multiple sources (database, files, user input)
> so need to handle different encodings, the above code fails on the first
> stample because the last '-'.
>
> I don't understand regex that well and could do with a hand.
>
> Thank you.
>
> Regards,
> Gary Harpin
>
> ________________________________
>
> "...a computer is a stupid machine with the ability to do incredibly
> smart things, while computer programmers are smart people with the
> ability to do incredibly stupid things. They are, in short, a perfect
> match.." Bill Bryson
>
> [No Disclaimer]
>
>
> ===================================
> This list is hosted by DevelopMentorĀ® http://www.develop.com
>
> View archives and manage your subscription(s) at http://discuss.develop.com
>

===================================
This list is hosted by DevelopMentorĀ® http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Gary Harpin
2007-10-12 10:47:27 UTC
Permalink
Per Bolmstedt
2007-10-12 10:40:09 UTC
Permalink
Gary Harpin wrote:

> I am trying to use Regex.Replace() to clean a string before working on
> it.

For the record, regular expressions in .NET don't have any relation to "the
specifics of the C# and Managed C++ languages" (from this list's name). They're
part of the BCL. Furthermore, "Microsoft .NET Framework regular expressions
incorporate the most popular features of other regular expression
implementations such as those in Perl and awk" and specifically are "Designed
to be compatible with Perl 5 regular expressions", which means most regular
expression documentation applies. But all the same, ".NET Framework regular
expressions include features not yet seen in other implementations, such as
right-to-left matching and on-the-fly compilation" (from the Developer's Guide).

All the same, I've found that using a Regular Expression IDE that lets you
instantly evaluate expressions, and in a sense "debug" them, is usually much
faster for learning than reading the documentation. I've used Expresso[1] on
occasion, but I don't know if there's something better out there.

In my experience, you rarely need to mix String.Replace() and RegEx.Replace();
one well-crafted regular expression usually does the job, and it can make your
code more coherent.

1: http://www.ultrapico.com/Expresso.htm

===================================
This list is hosted by DevelopMentorĀ® http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Alan Baljeu
2007-10-17 15:14:18 UTC
Permalink
Hewitt, Simon C. (Contractor)
2007-10-18 04:21:36 UTC
Permalink
Alan Baljeu
2007-10-18 14:16:51 UTC
Permalink
Hewitt, Simon C. (Contractor)
2007-10-19 04:37:16 UTC
Permalink
Alan Baljeu
2007-10-19 14:03:40 UTC
Permalink
Alan Baljeu
2007-10-17 19:24:32 UTC
Permalink
Shen, ShunMing
2007-10-17 20:27:34 UTC
Permalink
Loading...