regex to remove leading zeros and hypens?

S

Smokey Grindel

I basically need to remove any leading zeros and hyphens from a string...
regex seemed like the best rout and using a replace regex method... anyone
know of any good strings for this? The one I just made quick is ^0+|[-] but
not having much experience with regex, is this the correct one?
 
G

Göran Andersson

Smokey said:
I basically need to remove any leading zeros and hyphens from a
string... regex seemed like the best rout and using a replace regex
method... anyone know of any good strings for this? The one I just made
quick is ^0+|[-] but not having much experience with regex, is this the
correct one?

I would make a set that matches either a zero or a hyphen:

^[0\-]+

That could be used to replace this:

0--000-----0hello

into:

hello

Perhaps you want it to be more specific, like removing an optional
single hyphen followed by any number of zeroes?

^-?0*

This could be used to replace this:

-00001123

into:

1123

but it would leave this:

-00----00000Hi

as:

----00000Hi
 
E

eBob.com

Smokey Grindel said:
I basically need to remove any leading zeros and hyphens from a string...
regex seemed like the best rout and using a replace regex method... anyone
know of any good strings for this? The one I just made quick is ^0+|[-] but
not having much experience with regex, is this the correct one?

Your query is somewhat light on specifics, but if you have strictly leading
zeros and hyphens I think I would use String.TrimStart. That strikes me as
more straightforward than a regex replace. But if you want to play with
regex expressions get Expresso from UltraPico. It's free.

Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top