- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一些相似的字符串
例如:字符串 1:客户的名字是 john,他的姓是 glueck,他的公司名称是 abc def technolgies llc,他有 60 美元的余额。他的支出率为 +3.45%
字符串2:客户的名字是steve,他的姓是johnston,他的公司名称是xyz corporation,他有800美元的余额。他的消费率为-212.86%
现在我必须从字符串 1 中提取 john,glueck,abc def technolgies llc,60,+3.45 和从字符串 2 中提取 steve,johnston,xyz corporation,800,-212.86 等值。
在我们的生产环境中,每个字符串都非常大,我有大约 83 个字段要从每个字符串中提取。提取这些值的最佳方法是什么?
是否有任何方法与 string.format 相反,它采用引用字符串和实际字符串并返回提取的值?
最佳答案
正则表达式就可以了。
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
之后,您可以执行一些简单的字符串解析以从字符串中获取数值。此外,如果输入格式存在一些变化,您可能必须编写更健壮的正则表达式。
关于C#:如何有效地从预定义格式的字符串中提取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3247867/
我对我接管的项目有疑问。我正在转换其他人编写的 MS Access 应用程序并将其转换为 MySQL/PHP Web 应用程序。其中大部分已经完成,但是,当涉及到此应用程序的调度部分时,我处于停滞状态
我有一个带有 @Scheduled 注释的方法。此方法包含长时间运行、昂贵的操作。我担心当计划的方法开始运行时应用程序会变慢。有什么办法可以为预定方法分配优先级吗?在 Spring 中启动低优先级后台
我的大学有一个预订项目房间的网站;但除非你很幸运或者半夜醒着,否则要订到房间并不容易。因此,我编写了一个 JS 片段来填写所有必要的字段并提交表单。 但是我如何自动化这个过程呢? 我的目的基本上是加载
我正在评估处理大量排队消息的可能解决方案,这些消息必须在特定日期和时间交付给工作人员。执行它们的结果主要是对存储数据的更新,它们最初可能是也可能不是由用户操作触发的。 例如,想想你在一个假设的大型星际
@Scheduled documentation here声明 fixedRateString值可以是 the delay in milliseconds as a String value, e.g
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 4年前关闭。 Improve t
我有一个有趣的情况。我解析了几个新闻发布网站,想通过调度程序将它们保存到数据库中。但是保存时出现错误。由于交易后写条件 described here . 我的模型类是 @Entity @Table(n
我正在阅读 Java Concurrency in Practice 并遇到以下代码片段。 public static void timedRun(final Runnable r,
使用 Azure 数据工厂,是否可以对驻留在 Azure SQL 数据库中的多个(不是全部)表中的所有行执行预定的 1:1 复制/克隆到另一个 Azure SQL 数据库(在本例中为 Azure SQ
我是一名优秀的程序员,十分优秀!