简体   繁体   English

在C#中,从字符串中解析出这个值的最佳方法是什么?

[英]In C#, what is the best way to parse out this value from a string?

I have to parse out the system name from a larger string. 我必须从更大的字符串中解析出系统名称。 The system name has a prefix of "ABC" and then a number. 系统名称的前缀为“ABC”,然后是数字。 Some examples are: 一些例子是:

ABC500
ABC1100
ABC1300

the full string where i need to parse out the system name from can look like any of the items below: 我需要解析系统名称的完整字符串可以看起来像下面的任何项目:

ABC1100 - 2ppl
ABC1300
ABC 1300
ABC-1300
Managers Associates Only (ABC1100 - 2ppl)

before I saw the last one, i had this code that worked pretty well: 在我看到最后一个之前,我有一个非常好的代码:

string[] trimmedStrings = jobTitle.Split(new char[] { '-', '–' },StringSplitOptions.RemoveEmptyEntries)
                           .Select(s => s.Trim())
                           .ToArray();

return trimmedStrings[0];

but it fails on the last example where there is a bunch of other text before the ABC. 但是在最后一个例子中,它在ABC之前有一堆其他文本失败了。

Can anyone suggest a more elegant and future proof way of parsing out the system name here? 任何人都可以建议在这里解析系统名称的更优雅和未来的方法吗?

One way to do this: 一种方法:

string[] strings =
{
    "ABC1100 - 2ppl",
    "ABC1300",
    "ABC 1300",
    "ABC-1300",
    "Managers Associates Only (ABC1100 - 2ppl)"
};

var reg = new Regex(@"ABC[\s,-]?[0-9]+");

var systemNames = strings.Select(line => reg.Match(line).Value);

systemNames.ToList().ForEach(Console.WriteLine);

prints: 打印:

ABC1100
ABC1300
ABC 1300
ABC-1300
ABC1100

demo 演示

You really could leverage a Regex and get better results. 你真的可以利用正则表达式并获得更好的结果。 This one should do the trick [A-Za-z]{3}\\d+ , and here is a Rubular to prove it . 这个应该做的伎俩[A-Za-z]{3}\\d+ ,这里有一个Rubular来证明它 Then in the code use it like this: 然后在代码中使用它像这样:

var matches = Regex.Match(someInputString, @"[A-Za-z]{3}\d+");
if (matches.Success) {
    var val = matches.Value;
}

You can use a regular expression to parse this. 您可以使用正则表达式来解析它。 There may be better expressions, but this one works for your case: 可能有更好的表达方式,但这个适用于您的情况:

using System;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      string txt="ABC500";

      string re1="((?:[a-z][a-z]+))";   
      string re2="(\\d+)"

      Regex r = new Regex(re1+re2,RegexOptions.IgnoreCase|RegexOptions.Singleline);
      Match m = r.Match(txt);
      if (m.Success)
      {
            String word1=m.Groups[1].ToString();
            String int1=m.Groups[2].ToString();
            Console.Write("("+word1.ToString()+")"+"("+int1.ToString()+")"+"\n");
      }
    }
  }
}

You should definitely use Regex for this. 你绝对应该使用正则Regex Depending on the exact nature of the system name, something like this could prove to be enough: 根据系统名称的确切性质,这样的事情可以证明是足够的:

Regex systemNameRegex = new Regex(@"ABC[0-9]+");

If the ABC part of the name can change, you can modify the Regex to something like this: 如果名称的ABC部分可以更改,您可以将Regex修改为如下所示:

Regex systemNameRegex = new Regex(@"[a-zA-Z]+[0-9]+");

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

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