简体   繁体   English

使用Regex C#从字符串中提取值

[英]Extract value from a string using Regex C#

I will know if it's possible to extract value(int) from a string with regex in C#. 我将知道是否有可能在C#中使用正则表达式从字符串中提取value(int)。

For example, i have some datas like : 例如,我有一些数据,例如:

"124521test45125 100KG10 fdfdfdf" “ 124521test45125 100KG10 fdfdfdf”

"9856745test123456 60ML450 fdfdfdf" “ 9856745test123456 60ML450 fdfdfdf”

I will like to extract value which contain caracters "KG" and "ML" with numbers which are before and after this caracters. 我想提取包含“ KG”和“ ML”字样的值,这些数字带有该字样前后的数字。

The result will be "100KG10" and "60ML450". 结果将为“ 100KG10”和“ 60ML450”。

It is possible that they don't have numbers after like this : 他们可能没有这样的数字:

"124521test45125 100KG fdfdfdf" “ 124521test45125 100KG fdfdfdf”

The result for this case will be "100KG". 这种情况下的结果将是“ 100KG”。

I use this method to extract value : 我使用这种方法来提取价值:

public static string Test(string str)
        {
            Regex regex = new Regex(@"???REGEX??");
            Match match = regex.Match(str);
            if (match.Success)
                return match.Value;

            return match;
        }

The problem is i just start to learn Regex and i don't know how i can extract only this value. 问题是我刚刚开始学习Regex,我不知道如何只能提取此值。

Could any one help me. 谁能帮我。

Thanks in advance 提前致谢

I suggest the pattern to be 我建议模式是

 [0-9]+(KG|ML)[0-9]*

where 哪里

  • [0-9]+ one or more digits [0-9]+一位或多位数字
  • (KG|ML) followed by either KG or ML (KG|ML)然后是KGML
  • [0-9]* followed by zero or more digits [0-9]*后跟零个或多个数字

The implementation can be 实现可以是

public static string Test(string str) {
  // public methods should validate their values; null string is a case
  if (string.IsNullOrEmpty(str))
    return null;

  var match = Regex.Match(str, @"[0-9]+(KG|ML)[0-9]*");

  return match.Success ? match.Value : null;
}

RegEx 正则表达式

public static string Test(string str)
{
    Regex regex = new Regex(@"\d+(ML|KG)\d*");
    Match match = regex.Match(str);
    if (match.Success)
        return match.Value;

    return null;
}
string[] arrayResult = input.Split(' ').Where(x=>x.Contains("ML") || x.Contains("KG")).ToArray();

string result = string.Join(", ", arrayResult);

Here without regex. 这里没有正则表达式。

EDIT: After the comment: 编辑:评论后:

    public static bool Condition(string x)
    {
        bool check1 = x.Contains("ML");
        bool check2 = x.Contains("KG");
        bool result = false;

        if(check1)
        {
            x = x.Replace("ML", "");

            var arr = x.Where(y => !char.IsDigit(y));

            result = arr.Count() == 0;
        }
        else if(check2)
        {
            x = x.Replace("KG", "");

            var arr = x.Where(y => !char.IsDigit(y));

            result = arr.Count() == 0;

        }

        return result;
    }

    public static void Main(string[] args)
    {
        string input = "124521teKGst45125 100KG10 fdfdfdf";

        string[] arrayResult = input.Split(' ').Where(x => Condition(x)).ToArray();

        string result = string.Join(", ", arrayResult);
    }

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

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