Other Software > Developer's Corner
Need help with some string manip in C#
mwb1100:
I think the following algorithm will get you what you want (or close):
- remove any characters that are not an alpha, a digit, or a decimal point
- replace runs of alpha characters with a space
Now you have a space-delimited string of the numbers you're looking for.
Some things that might need work or modification:
- if you want to allow negative numeric values, then you might need to handle the minus sign specially (you discarded it in my question's example, so I figure you're not interested in negative values)
- if you want to allow a comma to be used as a decimal separator
But I think the basic idea gets you what you're looking for.
mouser:
I think a regex is the way to go. Easy to make one that ignores whitespaces, etc.
Renegade:
That looks workable, Ren. But any regex would need to capture and replace whitespace before, among and after the numbers: "9. 07" becomes "9.07", and " 7 6 . 04" becomes "76.04"
-kyrathaba (June 21, 2014, 07:13 PM)
--- End quote ---
While you can do something like:
( )?([0-9 ]+)\.([0-9 ]+)( )?
You can also concat the capture groups and use string.Trim() or string.Replace(). That's simple and reliable.
kyrathaba:
Yeah, I installed Regulator, but I really need to do some studying of regexes.
wraith808:
RegexHero is another you might want to try. Regexbuddy is the best Regex editor that I've come across.
Navigation
[0] Message Index
[*] Previous page
Go to full version