I want to remove everything after and including the decimal, and everything non-numerical except the hyphen if it's the first character. So far I have this: /[^0-9^-]|[^\.]+$/
. Notice how I block dashes from being removed with ^-
, because somehow I want to only remove the dashes that aren't the first character (not the sign). Any help? Thanks.
I just want it to remove
- Any non 0-9 characters, except the the first character if it is a dash (negative sign)
- Everything after and including the decimal point
Ex.:
10js-_67.09090FD
=> 1067
-10a.h96
=> -10
EDIT: Never mind, I was approaching this the wrong way, trying to match the characters that don't belong, and I realized I shouldn't be using a regex for this. Thanks for your answers though, I learned a bit about regex and maybe someone else with a similar problem will find this.
I want to remove everything after and including the decimal, and everything non-numerical except the hyphen if it's the first character. So far I have this: /[^0-9^-]|[^\.]+$/
. Notice how I block dashes from being removed with ^-
, because somehow I want to only remove the dashes that aren't the first character (not the sign). Any help? Thanks.
I just want it to remove
- Any non 0-9 characters, except the the first character if it is a dash (negative sign)
- Everything after and including the decimal point
Ex.:
10js-_67.09090FD
=> 1067
-10a.h96
=> -10
EDIT: Never mind, I was approaching this the wrong way, trying to match the characters that don't belong, and I realized I shouldn't be using a regex for this. Thanks for your answers though, I learned a bit about regex and maybe someone else with a similar problem will find this.
Share edited Sep 22, 2014 at 11:45 mk12 asked Oct 23, 2009 at 1:57 mk12mk12 26.5k34 gold badges100 silver badges140 bronze badges 02 Answers
Reset to default 2Try this:
Regex numbers = new Regex(@"^(-?\d*)[^0-9]*(\d*)\.",
RegexOptions.ECMAScript | RegexOptions.Multiline);
foreach (Match number in numbers.Matches("10js-_67.09090FD"))
{
Console.WriteLine(
Int32.Parse(
number.Groups[1].Value +
number.Groups[2].Value));
}
Or this one:
Console.WriteLine(
Int32.Parse(
Regex.Replace(
"10js-_67.09090FD",
@"^(-?\d*)[^0-9]*(\d*)\.([\s\S]*?)$", "$1$2",
RegexOptions.ECMAScript | RegexOptions.Multiline)));
Or this one:
var re = /^(-?\d*)[^0-9]*(\d*)\.([\s\S]*?)$/
alert(parseInt("10js-_67.09090FD".replace(re, "$1$2"),10));
That would be /^(-?[0-9]+)[^0-9\.]*([0-9]*).*$/\1\2/
(use for sed as you don't tell me what language ar you using).
/^(-?[0-9]+)[^0-9\.]*([0-9]*).*$/
// '^' ==>l From the Start
// '(..)' ==>l Group 1
// '-?' ==>l An optiona '-'
// '[0-9]+' ==>l Some numbers
// '[^0-9\.]*' ==>l Anything but numbers and dot
// '(..)' ==>l Group 2 (So this is the number after the dot)
// '[0-9]*' ==>l Some numbers
// '.*$' ==>l The rest
Then Only print Group 1 and Group 2 (/\1\2/).
Tests:
$:~/Desktop$ echo "10js-_67.09090FD" | sed -r "s/^(-?[0-9]+)[^0-9\.]*([0-9]*).*$/\1\2/"
1067
$:~/Desktop$ echo "-10a.h96" | sed -r "s/^(-?[0-9]+)[^0-9\.]*([0-9]*).*$/\1\2/"
-10
Hope this helps