嗨,我需要一个Regex表达式,它只从右到左提取浮点数。
示例字符串
每股收益(
) face value of2 each26 1,675.10 1,252.56 )
我现在的Regex
带有Rex选项的(\+|-)?[0-9][0-9]*(\,[0-9]*)?(\.[0-9]*)? -从右到左但是
电流输出
1,252.56
1,675.10
26
2但是,我不想在26或2上匹配
请帮帮我
发布于 2014-12-10 07:27:21
也许像这样的东西会有帮助
Regex
/[-+]?[0-9,\.]*([,\.])[0-9]*/g
示例输入
收入-34 5 b4 pe8r blah4 t3st +- (in) 1,252.56 face 12234,23423.342 1,675.10 1,252.56
匹配
1,252.56
-12234,23423.342
1,675.10
1,252.56解释
[-+]?匹配下面列表中的单个字符
?在0和one time之间,尽可能多次,贪婪地回馈-+列表中的单个字符-+字面意思是[0-9,\.]*匹配下面列表中的单个字符
*介于0和无限制的次数之间,尽可能多次,贪婪地回馈0-9在0和9之间范围内的单个字符,文字字符,\.与字符.匹配。第一捕捉群([,\.])
[,\.]匹配下面列表中的单个字符,文字字符,\.与字符.匹配。
[0-9]*匹配下面列表中的单个字符
*介于0和无限制的次数之间,尽可能多次,贪婪地回馈0-9在0和9之间范围内的单个字符g修饰符:全局。所有比赛(第一场比赛不要返回)
发布于 2014-12-10 23:24:00
虽然这是一个Regex问题,但它也被称为C#。
下面是一个例子,说明如何对输出进行更多的控制。
它也是特定于文化的,只取小数位的数字,没有假阳性。
方法
private List<double> GetNumbers(string input)
{
// declare result
var resultList = new List<double>();
// if input is empty return empty results
if (string.IsNullOrEmpty(input))
{
return resultList;
}
// Split input in to words, exclude empty entries
var words = input.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
// set your desirted culture
var culture = CultureInfo.CreateSpecificCulture("en-GB");
// Refine words into a list that represents potential numbers
// must have decimal place, must not start or end with decimal place
var refinedList = words.Where(x => x.Contains(".") && !x.StartsWith(".") && !x.EndsWith("."));
foreach (var word in refinedList)
{
double value;
// parse words using designated culture, and the Number option of double.TryParse
if (double.TryParse(word, NumberStyles.Number, culture, out value))
{
resultList.Add(value);
}
}
return resultList;
}使用
var testString = "Earning -34 5 b4 , . 234. 234, ,345 45.345 $234234 234.3453.345 $23423.2342 +234 -23423 pe8r blah4 t3st + - (in) 1,252.56 face -12234,23423.342 of 1,675.10 1,252.56";
var results = GetNumbers(testString);
foreach (var item in results)
{
Debug.WriteLine("{0}", item);
}输出
45.345
1252.56
-1223423423.342
1675.1
1252.56附加备注
您可以了解有关double.TryParse及其选项这里的更多信息。
您可以了解有关CultureInfo类这里的更多信息。
https://stackoverflow.com/questions/27395059
复制相似问题