正则表达式只从字符串获取NUMBER [英] Regex to get NUMBER only from String

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

问题描述

我收到7+或5+或+5,从XML,并希望只提取使用正则表达式的从弦数。
例如Regex.Match()函数

  stringThatHaveCharacters = stringThatHaveCharacters.Trim();
        匹配M = Regex.Match(stringThatHaveCharacters,我在这里使用);
        INT数= Convert.ToInt32(m.Value);
        返回数;


解决方案

\\ D +

\\ D 重新presents任何数字, + 为一个或多个。如果你想赶上负数,以及可以使用 - ?\\ D +

请注意,作为一个字符串,它应该重新在C#psented $ P $为\\\\ D + @\\ D +

I recieve "7+" or "5+" or "+5" from XML and wants to extract only the number from string using Regex. e.g Regex.Match() function

        stringThatHaveCharacters = stringThatHaveCharacters.Trim();
        Match m = Regex.Match(stringThatHaveCharacters, "WHAT I USE HERE");
        int number = Convert.ToInt32(m.Value);
        return number;

解决方案

\d+

\d represents any digit, + for one or more. If you want to catch negative numbers as well you can use -?\d+.

Note that as a string, it should be represented in C# as "\\d+", or @"\d+"

这篇关于正则表达式只从字符串获取NUMBER的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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