代码之家  ›  专栏  ›  技术社区  ›  funwithcoding

C#:如何有效地从预定义的字符串格式中提取值?

  •  1
  • funwithcoding  · 技术社区  · 14 年前

    我收集了一些类似的弦

    例如: 字符串1:客户的名字是john,姓glueck,公司名是abc def technolgies llc,余额为60美元,消费率为+3.45%

    字符串2:客户的名字是史蒂夫,他的姓是约翰斯顿,他的公司名是xyz公司,他有800美元的余额。他的消费率是-212.86%

    现在我必须从字符串1中提取john,glueck,abc def technolgies llc,60,+3.45和steve,johnston,xyz corporation,800,-212.86这样的值。

    是否有与string.format相反的方法,该方法接受引用字符串&实际字符串并返回提取的值?

    2 回复  |  直到 14 年前
        1
  •  13
  •   Daniel Brückner Pradip    14 年前

    一个正则表达式就可以了。

    namespace ConsoleApplication
    {
        using System;
        using System.Text.RegularExpressions;
    
        internal static class Program
        {
            private static void Main()
            {
                var expression = new Regex(
                    @"Customer's first Name is (?<FirstName>[^,]+), " +
                    @"his last name is (?<LastName>[^,]+), " +
                    @"his company name is (?<CompanyName>[^,]+), " +
                    @"he has a balance of (?<Balance>[0-9]+) dollars\. " +
                    @"His spending rate is (?<SpendingRate>[^%]+)%");
    
                var line = @"Customer's first Name is john, his last name is glueck, his company name is abc def technolgies llc, he has a balance of 60 dollars. His spending rate is +3.45%";
    
                var match = expression.Match(line);
    
                Console.WriteLine("First name......{0}", match.Groups["FirstName"]);
                Console.WriteLine("Last name.......{0}", match.Groups["LastName"]);
                Console.WriteLine("Balance.........{0}", match.Groups["Balance"]);
                Console.WriteLine("Spending rate...{0}", match.Groups["SpendingRate"]);
    
                Console.ReadLine();
            }
        }
    }
    

    First name......john
    Last name.......glueck
    Balance.........60
    Spending rate...+3.45
    

    之后,您可以执行一些简单的字符串解析,从字符串中获取数值。此外,如果输入的格式有一些变化,您可能需要编写一个更健壮的正则表达式。

        2
  •  2
  •   James Curran    14 年前

    (问题:您实际输入的字符串是完整的文字:“客户的名字是xxxx,他的姓是xxxx,他的公司名是xxxx”等等。正确吗?)

    更新:

      // NOTE: pattern assumes a comma after spending rate
      Regex regex = new Regex("Customer's first Name is (\w+), his last name is (\w+),his company name is ([\w\s]+), he has a balance of (\d+) dollars.His spending rate is ([^,]+)");
    
      string[] values = regex.Split(string1);