从C#中的字符串中提取数字 [英] Extract numbers from a string in C#

查看:99
本文介绍了从C#中的字符串中提取数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好..我有一台以这种格式通过串口发送数据的机器。

_____________________

HSU-avg = 00(HSU)

K-avg = 0.01

9 1930 2.8 0.01

8 1940 2.8 0.01

7 2190 2.8 0.01

6 1810 2.8 0.01

5 1970 18.8 0.02

4 2190 2.8 0.01

3 2150 2.8 0.01

2 2070 13.1 0.01

1 2150 25.7 0.02

------- ---- ----

MAX.RPM SEC。 K.

OIL.TEMP = 0088

-----------------------

AVG:1040 2065

-------- -------

6 0830 2080

5 0660 1890

4 1340 1960

3 1330 2320

2 0690 2170

1 0840 2190
-------- -------

IDLE.RPM MAX.RPM

-------- ---------------



我目前使用此

 Regex.Split(machinedata,< span class =code-string> @  [^ 0-9 \。] +)。其中(c = >  c!=  && c.Trim()!=  );     

将数字提取到数组中然后计算它们。还有其他更好的方法吗???



我的问题不在于正则表达式(正则表达式正常)。我是初学者,S o我想知道最好的做法,例如你如何处理这类事情。??

解决方案

这可以帮助你检查你的RegEx做什么您的期望。

perlre - perldoc.perl.org [ ^ ]

Debuggex:在线视觉正则表达式测试器。 JavaScript,Python和PCRE。 [ ^ ]


请参考下面的链接,它可能会回答你的问题。

http://www.codeproject.com/Articles/9099/The-Minute-Regex-Tutorial

Hello everyone .. I have a machine that sends data through serial port in this format.
_____________________
HSU-avg = 00 (HSU)
K-avg = 0.01
9 1930 2.8 0.01
8 1940 2.8 0.01
7 2190 2.8 0.01
6 1810 2.8 0.01
5 1970 18.8 0.02
4 2190 2.8 0.01
3 2150 2.8 0.01
2 2070 13.1 0.01
1 2150 25.7 0.02
------- ---- ----
MAX.RPM SEC. K.
OIL.TEMP = 0088
-----------------------
AVG :1040 2065
-------- -------
6 0830 2080
5 0660 1890
4 1340 1960
3 1330 2320
2 0690 2170
1 0840 2190
-------- -------
IDLE.RPM MAX.RPM
-----------------------

I currently use this

Regex.Split(machinedata, @"[^0-9\.]+").Where(c => c != "." && c.Trim() != "");"

to extract the numbers into an array and then calculate them. Is there any other better way .. ??

My problem is not with the regex(regex works fine) . I am a beginner, So I would like to know the best practices like how you pro's deal with this sort of thing ..??

解决方案

This may help you to check that your RegEx do what you expect.
perlre - perldoc.perl.org[^]
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]


Please refer link below it might be give answer of your question.
http://www.codeproject.com/Articles/9099/The-Minute-Regex-Tutorial


这篇关于从C#中的字符串中提取数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆