Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#:如何有效地从预定义的字符串格式中提取值?_C#_.net - Fatal编程技术网

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

C#:如何有效地从预定义的字符串格式中提取值?,c#,.net,C#,.net,我收集了一些类似的字符串 例如: 字符串1:客户的名字是john,姓氏是glueck,公司名是abc def Technogies llc,余额为60美元。支出率为+3.45% 字符串2:客户的名字是史蒂夫,他的姓是约翰斯顿,他的公司名是xyz公司,他有800美元的余额。他的消费率是-212.86% 现在我必须从字符串1中提取像john,glueck,abc def Technologies llc,60,+3.45和steve,johnston,xyz corporation,800,-212

我收集了一些类似的字符串

例如: 字符串1:客户的名字是john,姓氏是glueck,公司名是abc def Technogies llc,余额为60美元。支出率为+3.45%

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

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

在我们的生产环境中,每个字符串都相当大,我要从每个字符串中提取大约83个字段。提取这些值的最佳方法是什么

是否有与string.format相反的方法,该方法获取参考字符串和实际字符串并返回提取的值?

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

对于正则表达式来说,这可能是一个很好的例子。如果您使用compile选项,您应该可以从中获得合理的速度。基本上是您询问的“reverse string.format”(还有更多选项)

更新:

  // 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);  
(问题:您实际输入的字符串是完整的文字:“客户的名字是xxxx,他的姓是xxxx,他的公司名是xxxx”等等。正确吗?)

对于正则表达式来说,这可能是一个很好的例子。如果您使用compile选项,您应该可以从中获得合理的速度。基本上是您询问的“reverse string.format”(还有更多选项)

更新:

  // 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);  

一个正则表达式就可以做到这一点

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();
        }
    }
}

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

正则表达式可以实现这一点

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();
        }
    }
}

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

您能提供一个字符串的实际示例吗?起初,你说它看起来像一个句子,但随后你给出了一些逗号分隔的值。我相信CSV字符串显示了所需的输出。你能提供一个字符串的实际示例吗?起初,你说它看起来像一个句子,但随后你给出了一些逗号分隔的值。我相信CSV字符串显示了所需的输出。James,正则表达式似乎是一种方法。你能和我分享一下我的示例字符串1应该使用的正则表达式吗?James,正则表达式似乎是一个不错的选择。你能给我分享一下我应该用于示例字符串1的正则表达式吗?