【问题标题】:String Replace & Split字符串替换和拆分
【发布时间】:2021-09-12 03:30:09
【问题描述】:

如何使用单个 Replace & Spit 方法拆分此值

Tel-0190 Texas 2020-12-31 9 890,00 $ 4,00 $ 8 690,00 $

我想像这样拆分字符串结果:

"Tel-0190" "Texas" "2020-12-31" "9 890,00 $" "4,00 $" "8 690,00 $" 

我试过了:

str.Replace(" ","_")
   .Replace("\d* ","\d* ")
   .Replace(" €"," €")
   .Split("_"C)

【问题讨论】:

  • Tel-0190 Texas 2020-12-31 9 890,00 $ 4,00 $ 8 690,00 $
  • 我想用 String.Replace & Split 方法像这样分开分割
  • "Tel-0190" "Texas" "2020-12-31" "9 890,00 $" "4,00 $" "8 690,00 $"
  • 使用正则表达式......
  • 数字和货币符号中的空格会让这变得比原来更难

标签: c# .net string rpa uipath


【解决方案1】:

这是一个使用正则表达式的尝试:

private const string Source = "Tel-0190 Texas 2020-12-31 9 890,00 $ 4,00 $ 8 690,00 $";

private const string RegexPattern =
        @"(?<tel>Tel-\d+) (?<state>Texas) (?<date>\d{4}-\d{1,2}-\d{1,2}) (?<num1>[0-9, ]+[$€]) (?<num2>[0-9, ]+[$€]) (?<num3>[0-9, ]+[$€])";

我在正则表达式中使用“命名组”。我试图猜测你的规则。此代码将找到组:

var regex = new Regex(RegexPattern);
var match = regex.Match(Source);
if (match != null && match.Groups.Count == 7)
{
    var groups = match.Groups;
    Debug.WriteLine(groups["tel"]);
    Debug.WriteLine(groups["state"]);
    Debug.WriteLine(groups["date"]);
    Debug.WriteLine(groups["num1"]);
    Debug.WriteLine(groups["num2"]);
    Debug.WriteLine(groups["num3"]);
}

结果如下:

Tel-0190
Texas
2020-12-31
9 890,00 $
4,00 $
8 690,00 $

【讨论】:

  • 只是一个猜测,但我怀疑它应该完全匹配“德克萨斯”。
  • @juharr:这可能是一个很好的猜测,但问题中没有其他信息可以继续
猜你喜欢
  • 2012-03-22
  • 2021-12-23
  • 1970-01-01
  • 2021-05-31
  • 1970-01-01
  • 2020-12-02
  • 2016-11-08
  • 1970-01-01
  • 2013-04-26
相关资源
最近更新 更多