|
-
Dec 11th, 2006, 07:46 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] Regex problem
Hi,
I've used the below regex expresssion to strip all the characters from a string value and only return the numbers.
Code:
String.Join(Nothing, System.Text.RegularExpressions.Regex.Split(strValue, "[^\d]"))
If the input is: abc12a
Output is: 12
The problem is that if I pass abc12.23a then the output is 1223 while I want is 12.23
Can somebody suggest what should I do?
Thanks.
-
Dec 11th, 2006, 08:11 AM
#2
Hyperactive Member
Re: Regex problem
Well And what if Input is : abc12d34 ???
I am using .NET 2010 with Windows 7
-
Dec 11th, 2006, 08:40 AM
#3
Thread Starter
Fanatic Member
Re: Regex problem
Though interesting input, but that won't happen. The reason is that I'm trying to take price out of a string. So price can be $100,234.55MM etc but never in the format that you've specified. And if by any chance my method gets that and computes incorrectly, that won't be an application issue.
-
Dec 11th, 2006, 09:03 AM
#4
Re: Regex problem
How about "[0-9]*\.?[0-9]*" or to be more compact "[\d]*\.?[\d]*"
-
Dec 11th, 2006, 09:08 AM
#5
Re: Regex problem
how bout if you just do a replace on a-z characters?
VB Code:
System.Text.RegularExpressions.Regex.Replace("abc12.34ac", "[a-zA-Z]*", String.Empty)
-
Dec 11th, 2006, 01:54 PM
#6
Re: Regex problem
All you had to do originally is add in the "." to your pattern in your very first post. Since it is a special character, escape it with a slash, so the pattern you needed was just "[^\d\.]"
VB Code:
Dim strValue As String = "abc12.34abc"
MessageBox.Show(String.Join(Nothing, System.Text.RegularExpressions.Regex.Split(strValue, "[^\d\.]")))
-
Dec 12th, 2006, 06:06 AM
#7
Thread Starter
Fanatic Member
Re: Regex problem
Thanks gigemboy's. That provoked me to read a more about Regex. I think I'll take some more time to get a full hold on it.
@bmahler's solution won't work on $12.5MM.
Bulldog, you didn't even try to test the solution you proposed. It strips all the numbers from the string that is passed.
Thanks to all anyways who took the pain to help me out.
-
Dec 13th, 2006, 07:52 PM
#8
Re: [RESOLVED] Regex problem
Bulldog, you didn't even try to test the solution you proposed. It strips all the numbers from the string that is passed.
What I was showing you was how to match the number sequence you require thinking that you would use this to do a match. So rather than using a "split" which generates an array of segments to sort through, you could use a "match" and generate a match collection. It was tested in RegExBuddy btw.
You are currently separating out the string sequence you want by removing anything that isnt a "." or a number. This may work today, but for future proofing I would be inclined to match what you "know" to be there, rather than removing the "unknowns" around it. In the future, the database may end up with "Sold,3kilos,at,$125k" or "Note:-Refund,-35$" etc.
Over time you can easily make a regex match more specific, to deal with negative numbers you can add an optional "-" at the start of the expression, you could also deal with extra spaces, different currency symbols, or the use of a trailing "k" for thousand etc. etc. It would be extremely difficult to do this correctly by removing the characters you dont expect.
About half of my code is always consumed by error checking, a good mantra being assume nothing! and expect the unexpected!
Anyway just my $0.02...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|