从C#中的字符串获取数字 [英] Getting number from a string in C#

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

问题描述

我正在抓取类似这样的网站内容-公司股票7100卢比

I am scraping some website content which is like this - "Company Stock Rs. 7100".

现在,我想要的是从该字符串中提取数值。我尝试了split,但是我的正则表达式出了点问题。

Now, what i want is to extract the numeric value from this string. I tried split but something or the other goes wrong with my regular expression.

请让我知道如何获取此值。

Please let me know how to get this value.

推荐答案

使用:

var result = Regex.Match(input, @"\d+").Value;

如果只想查找字符串中最后一个实体的数字,则应使用此正则表达式:

If you want to find only number which is last "entity" in the string you should use this regex:

\d+$

如果要匹配字符串中的最后一个数字,可以使用:

If you want to match last number in the string, you can use:

\d+(?!\D*\d)

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

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