C# Regular Expressions

G

Guest

I have a series of number that need parsed. Strip the zeros, if a caret
exists replace it with a slash.

1.) 000123^2 - should be 123/2
2.) 1^2 - should be 1/2
3.) 0001 - should be 1
4.) 000123 - should be 123

I have this started:
Regex r = new Regex(@"(?<base>[1-9]{1}\d*)\W(?<group>\d*)");
Console.WriteLine(r.Match(value).Result("${base}/${group}"));

I do not know how to do the condtional to exclude the slash on examples 3
and 4. How do I prevent the addition of the slash when there is no caret in
the number?

Thanks!
 
G

Guest

Sorry,

it doesn't work for "00010^2"

Thanks though

Kevin Spencer said:
stringVariable.Replace('^', '/').Replace('0', '');

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

A watched clock never boils.

Stedak said:
I have a series of number that need parsed. Strip the zeros, if a caret
exists replace it with a slash.

1.) 000123^2 - should be 123/2
2.) 1^2 - should be 1/2
3.) 0001 - should be 1
4.) 000123 - should be 123

I have this started:
Regex r = new Regex(@"(?<base>[1-9]{1}\d*)\W(?<group>\d*)");
Console.WriteLine(r.Match(value).Result("${base}/${group}"));

I do not know how to do the condtional to exclude the slash on examples 3
and 4. How do I prevent the addition of the slash when there is no caret
in
the number?

Thanks!
 
K

Kevin Spencer

You'll have to explain what you mean by "it doesn't work."

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

A watched clock never boils.

Stedak said:
Sorry,

it doesn't work for "00010^2"

Thanks though

Kevin Spencer said:
stringVariable.Replace('^', '/').Replace('0', '');

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

A watched clock never boils.

Stedak said:
I have a series of number that need parsed. Strip the zeros, if a caret
exists replace it with a slash.

1.) 000123^2 - should be 123/2
2.) 1^2 - should be 1/2
3.) 0001 - should be 1
4.) 000123 - should be 123

I have this started:
Regex r = new Regex(@"(?<base>[1-9]{1}\d*)\W(?<group>\d*)");
Console.WriteLine(r.Match(value).Result("${base}/${group}"));

I do not know how to do the condtional to exclude the slash on examples
3
and 4. How do I prevent the addition of the slash when there is no
caret
in
the number?

Thanks!
 
J

Jon Skeet [C# MVP]

Stedak said:
Sorry,

it doesn't work for "00010^2"

Well, it does exactly what you originally specified - it strips all the
zeroes and changes the caret into a slash. I suspect what you *meant*
to say was that *leading* zeroes should be stripped, in which case you
can use

stringVariable.TrimStart('0').Replace('^', '/')
 
J

Jon Skeet [C# MVP]

Kevin Spencer said:
I suspected that too, Jon, but I wanted him to think through his analysis.

I reckoned your reply (and the start of mine) would be enough to
highlight that a specification needs to be thought through carefully -
but thought he might not be aware of the TrimStart method.
 

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