简体   繁体   English

从字符串中查找并提取数字

[英]Find and extract a number from a string

I have a requirement to find and extract a number contained within a string.我需要查找并提取字符串中包含的数字。

For example, from these strings:例如,从这些字符串:

string test = "1 test"
string test1 = " 1 test"
string test2 = "test 99"

How can I do this?我怎样才能做到这一点?

\\d+ is the regex for an integer number. \\d+是整数的正则表达式。 So所以

//System.Text.RegularExpressions.Regex
resultString = Regex.Match(subjectString, @"\d+").Value;

returns a string containing the first occurrence of a number in subjectString .返回一个字符串,其中包含在subjectString中第一次出现的数字。

Int32.Parse(resultString) will then give you the number. Int32.Parse(resultString)然后会给你这个数字。

这是我清理电话号码以仅获取数字的方法:

string numericPhone = new String(phone.Where(Char.IsDigit).ToArray());

go through the string and use Char.IsDigit遍历字符串并使用Char.IsDigit

string a = "str123";
string b = string.Empty;
int val;

for (int i=0; i< a.Length; i++)
{
    if (Char.IsDigit(a[i]))
        b += a[i];
}

if (b.Length>0)
    val = int.Parse(b);

use regular expression ...使用正则表达式...

Regex re = new Regex(@"\d+");
Match m = re.Match("test 66");

if (m.Success)
{
    Console.WriteLine(string.Format("RegEx found " + m.Value + " at position " + m.Index.ToString()));
}
else
{
    Console.WriteLine("You didn't enter a string containing a number!");
}

What I use to get Phone Numbers without any punctuation...我用什么来获取没有任何标点符号的电话号码...

var phone = "(787) 763-6511";

string.Join("", phone.ToCharArray().Where(Char.IsDigit));

// result: 7877636511

Regex.Split can extract numbers from strings. Regex.Split 可以从字符串中提取数字。 You get all the numbers that are found in a string.您将获得在字符串中找到的所有数字。

string input = "There are 4 numbers in this string: 40, 30, and 10.";
// Split on one or more non-digit characters.
string[] numbers = Regex.Split(input, @"\D+");
foreach (string value in numbers)
{
    if (!string.IsNullOrEmpty(value))
    {
    int i = int.Parse(value);
    Console.WriteLine("Number: {0}", i);
    }
}

Output:输出:

Number: 4 Number: 40 Number: 30 Number: 10人数:4 人数:40 人数:30 人数:10

Here's a Linq version:这是一个Linq版本:

string s = "123iuow45ss";
var getNumbers = (from t in s
                  where char.IsDigit(t)
                  select t).ToArray();
Console.WriteLine(new string(getNumbers));

Another simple solution using Regex You should need to use this使用正则表达式的另一个简单解决方案您应该需要使用它

using System.Text.RegularExpressions;

and the code is代码是

string var = "Hello3453232wor705Ld";
string mystr = Regex.Replace(var, @"\d", "");
string mynumber = Regex.Replace(var, @"\D", "");
Console.WriteLine(mystr);
Console.WriteLine(mynumber);

你也可以试试这个

string.Join(null,System.Text.RegularExpressions.Regex.Split(expr, "[^\\d]"));

Here is another Linq approach which extracts the first number out of a string.这是另一种从字符串中提取第一个数字的Linq方法。

string input = "123 foo 456";
int result = 0;
bool success = int.TryParse(new string(input
                     .SkipWhile(x => !char.IsDigit(x))
                     .TakeWhile(x => char.IsDigit(x))
                     .ToArray()), out result);

Examples:例子:

string input = "123 foo 456"; // 123
string input = "foo 456";     // 456
string input = "123 foo";     // 123

Just use a RegEx to match the string, then convert:只需使用正则表达式匹配字符串,然后转换:

Match match = Regex.Match(test , @"(\d+)");
if (match.Success) {
   return int.Parse(match.Groups[1].Value);
}
 string input = "Hello 20, I am 30 and he is 40";
 var numbers = Regex.Matches(input, @"\d+").OfType<Match>().Select(m => int.Parse(m.Value)).ToArray();

if the number has a decimal points, you can use below如果数字有小数点,您可以在下面使用

using System;
using System.Text.RegularExpressions;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            //Your code goes here
            Console.WriteLine(Regex.Match("anything 876.8 anything", @"\d+\.*\d*").Value);
            Console.WriteLine(Regex.Match("anything 876 anything", @"\d+\.*\d*").Value);
            Console.WriteLine(Regex.Match("$876435", @"\d+\.*\d*").Value);
            Console.WriteLine(Regex.Match("$876.435", @"\d+\.*\d*").Value);
        }
    }
}

results :结果 :

"anything 876.8 anything" ==> 876.8 “任何东西 876.8 任何东西”==> 876.8

"anything 876 anything" ==> 876 “任何东西 876 任何东西”==> 876

"$876435" ==> 876435 “$876435”==> 876435

"$876.435" ==> 876.435 “$876.435”==> 876.435

Sample : https://dotnetfiddle.net/IrtqVt示例: https : //dotnetfiddle.net/IrtqVt

For those who want decimal number from a string with Regex in TWO line:对于那些想要在行中使用正则表达式的字符串中的十进制数的人:

decimal result = 0;
decimal.TryParse(Regex.Match(s, @"\d+").Value, out result);

Same thing applys to float , long , etc...同样的事情适用于floatlong等......

You can do this using String property like below:您可以使用如下所示的String属性执行此操作:

 return new String(input.Where(Char.IsDigit).ToArray()); 

which gives only number from string.它只给出字符串中的数字。

var match=Regex.Match(@"a99b",@"\d+");
if(match.Success)
{
    int val;
    if(int.TryParse(match.Value,out val))
    {
        //val is set
    }
}

The question doesn't explicitly state that you just want the characters 0 to 9 but it wouldn't be a stretch to believe that is true from your example set and comments.该问题没有明确说明您只想要字符 0 到 9,但从您的示例集和评论中相信这是真的,这并不难。 So here is the code that does that.所以这是执行此操作的代码。

        string digitsOnly = String.Empty;
        foreach (char c in s)
        {
            // Do not use IsDigit as it will include more than the characters 0 through to 9
            if (c >= '0' && c <= '9') digitsOnly += c;
        }

Why you don't want to use Char.IsDigit() - Numbers include characters such as fractions, subscripts, superscripts, Roman numerals, currency numerators, encircled numbers, and script-specific digits.为什么不想使用 Char.IsDigit() - 数字包括诸如分数、下标、上标、罗马数字、货币分子、带圆圈的数字和特定于脚本的数字等字符。

var outputString = String.Join("", inputString.Where(Char.IsDigit));

Get all numbers in the string.获取字符串中的所有数字。 So if you use for examaple '1 plus 2' it will get '12'.因此,如果您使用例如“1 加 2”,它将得到“12”。

Here is another simple solution using Linq which extracts only the numeric values from a string.这是另一个使用Linq的简单解决方案,它仅从字符串中提取数值。

var numbers = string.Concat(stringInput.Where(char.IsNumber));

Example:例子:

var numbers = string.Concat("(787) 763-6511".Where(char.IsNumber));

Gives: "7877636511"给出:“7877636511”

Extension method to get all positive numbers contained in a string:获取字符串中包含的所有数的扩展方法:

    public static List<long> Numbers(this string str)
    {
        var nums = new List<long>();
        var start = -1;
        for (int i = 0; i < str.Length; i++)
        {
            if (start < 0 && Char.IsDigit(str[i]))
            {
                start = i;
            }
            else if (start >= 0 && !Char.IsDigit(str[i]))
            {
                nums.Add(long.Parse(str.Substring(start, i - start)));
                start = -1;
            }
        }
        if (start >= 0)
            nums.Add(long.Parse(str.Substring(start, str.Length - start)));
        return nums;
    }

If you want negative numbers as well simply modify this code to handle the minus sign ( - )如果您还想要负数,只需修改此代码以处理减号 ( - )

Given this input:鉴于此输入:

"I was born in 1989, 27 years ago from now (2016)"

The resulting numbers list will be:结果数字列表将是:

[1989, 27, 2016]

An interesting approach is provided here by Ahmad Mageed, uses Regex and StringBuilder to extract the integers in the order in which they appear in the string. Ahmad Mageed 在这里提供一个有趣的方法,它使用 Regex 和StringBuilder按照整数在​​字符串中出现的顺序提取它们。

An example using Regex.Split based on the post by Ahmad Mageed is as follows:基于 Ahmad Mageed 的帖子使用Regex.Split的示例如下:

var dateText = "MARCH-14-Tue";
string splitPattern = @"[^\d]";
string[] result = Regex.Split(dateText, splitPattern);
var finalresult = string.Join("", result.Where(e => !String.IsNullOrEmpty(e)));
int DayDateInt = 0;

int.TryParse(finalresult, out DayDateInt);

Did the reverse of one of the answers to this question: How to remove numbers from string using Regex.Replace?这个问题的答案之一是否与此相反: How to remove numbers from string using Regex.Replace?

// Pull out only the numbers from the string using LINQ

var numbersFromString = new String(input.Where(x => x >= '0' && x <= '9').ToArray());

var numericVal = Int32.Parse(numbersFromString);
  string verificationCode ="dmdsnjds5344gfgk65585";
            string code = "";
            Regex r1 = new Regex("\\d+");
          Match m1 = r1.Match(verificationCode);
           while (m1.Success)
            {
                code += m1.Value;
                m1 = m1.NextMatch();
            }

I have used this one-liner to pull all numbers from any string.我已经使用这个单线从任何字符串中提取所有数字。

var phoneNumber = "(555)123-4567";
var numsOnly = string.Join("", new Regex("[0-9]").Matches(phoneNumber)); // 5551234567

Here is my Algorithm这是我的算法

    //Fast, C Language friendly
    public static int GetNumber(string Text)
    {
        int val = 0;
        for(int i = 0; i < Text.Length; i++)
        {
            char c = Text[i];
            if (c >= '0' && c <= '9')
            {
                val *= 10;
                //(ASCII code reference)
                val += c - 48;
            }
        }
        return val;
    }
static string GetdigitFromString(string str)
    {
        char[] refArray = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
        char[] inputArray = str.ToCharArray();
        string ext = string.Empty;
        foreach (char item in inputArray)
        {
            if (refArray.Contains(item))
            {
                ext += item.ToString();
            }
        }
        return ext;
    }

here is my solution这是我的解决方案

string var = "Hello345wor705Ld";
string alpha = string.Empty;
string numer = string.Empty;
foreach (char str in var)
{
    if (char.IsDigit(str))
        numer += str.ToString();
    else
        alpha += str.ToString();
}
Console.WriteLine("String is: " + alpha);
Console.WriteLine("Numeric character is: " + numer);
Console.Read();
string s = "kg g L000145.50\r\n";
        char theCharacter = '.';
        var getNumbers = (from t in s
                          where char.IsDigit(t) || t.Equals(theCharacter)
                          select t).ToArray();
        var _str = string.Empty;
        foreach (var item in getNumbers)
        {
            _str += item.ToString();
        }
        double _dou = Convert.ToDouble(_str);
        MessageBox.Show(_dou.ToString("#,##0.00"));

You will have to use Regex as \\d+您将不得不使用 Regex 作为\\d+

\\d matches digits in the given string. \\d匹配给定字符串中的数字。

Using @tim-pietzcker answer from above , the following will work for PowerShell .使用上面的@tim-pietzcker 回答,以下内容适用于PowerShell

PS C:\> $str = '1 test'
PS C:\> [regex]::match($str,'\d+').value
1

Using StringBuilder is slightly more performent than string concatanation in a loop.在循环中使用 StringBuilder 比字符串串联稍微好一点。 If you are dealing with large strings, it is considerably more performant.如果您正在处理大字符串,它的性能要高得多。

    public static string getOnlyNumbers(string input)
    {
        StringBuilder stringBuilder = new StringBuilder(input.Length);
        for (int i = 0; i < input.Length; i++)
            if (input[i] >= '0' && input[i] <= '9')
                stringBuilder.Append(input[i]);

        return stringBuilder.ToString();
    }

Note: above example function only works for positive numbers注意:上面的示例函数仅适用于正数

Based on the last sample I created a method:基于上一个示例,我创建了一个方法:

private string GetNumberFromString(string sLongString, int iLimitNumbers)
{
    string sReturn = "NA";
    int iNumbersCounter = 0;
    int iCharCounter = 0; 

    string sAlphaChars = string.Empty;
    string sNumbers = string.Empty;
    foreach (char str in sLongString)
    {
        if (char.IsDigit(str))
        {
            sNumbers += str.ToString();
            iNumbersCounter++;
            if (iNumbersCounter == iLimitNumbers)
            {
                return sReturn = sNumbers;
            }
        }
        else
        {
            sAlphaChars += str.ToString();
            iCharCounter++;
            // reset the counter 
            iNumbersCounter = 0; 
        }
    }
    return sReturn;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM