【问题标题】:How to search for immediate Substring present at multiple places in c# [duplicate]如何在c#中搜索多个位置存在的立即子字符串[重复]
【发布时间】:2014-02-19 10:14:47
【问题描述】:

我有一个字符串,我需要拆分成多个子字符串。现在根据我的条件,同时拆分成子字符串,我必须在字符串中搜索两个文本。这是我的两个文本..

  1. 1 . 2 To Other Mobiles(可能会根据子串需要的条件而改变)
  2. Total(这是子串的最后一个)

这是我的示例字符串内容

1 . 1 To Airtel Mobile
1 03/MAR/2013 16:06:31 9845070641 05:44 1.80 **
2 04/MAR/2013 10:00:29 9845096416 00:14 0.30 **
Total 25:28 9.30
1 . 2 To Other Mobiles
1 03/MAR/2013 06:41:06 9448485859 00:15 0.40 **
2 04/MAR/2013 18:57:47 9448367847 08:33 3.60 **
3 05/MAR/2013 18:57:05 9448485859 00:42 0.40 **
4 05/MAR/2013 20:13:19 9448367847 00:42 0.40 **
Total 34:33 18.00
1 . 3 To Fixed Landline
21 21/MAR/2013 11:59:35 08066000000 09:34 5.00
22 22/MAR/2013 11:31:33 08066000000 15:20 8.00
Total 01:35:23 54.00

基于这两个文本的索引,我将字符串分解为子字符串。现在根据我的情况,我必须将子字符串读取到直接文本,即Total

但在我目前的代码中,我正在读取字符串到子字符串,直到最后一个 Total 文本。

这是我的代码:

string search1 = "1 . 2 To Other Mobiles";
string search2 = "Total";

int startPosition = currentText.IndexOf(search1);
if (startPosition >= 0)
{
    startPosition += search1.Length;
    int endPosition = currentText.LastIndexOf(search2);
    if (endPosition > startPosition)
    {
        string result = currentText.Substring(startPosition, endPosition - startPosition);
    }
}

简而言之,我必须搜索第一个可搜索文本并开始阅读直到第二个文本。

【问题讨论】:

  • 您之前的问题会发生什么。 stackoverflow.com/q/21874852/3184380
  • @Nimesh 它正在读取到文本“Total”的最后一个位置,而我需要它到直接的位置..
  • 所以您不想在子字符串中包含“Total”?
  • 即。如果字符串是“1 . 2 To Other Mobiles sdas askdka sdsajhsjdsj dsjdhajdhs Total 2541/-” 那么应该返回什么结果?
  • @Nimesh 是的,我不想包含并且只想阅读到第一个“总计”。无需获取最后一个“总计”的位置或索引。从“1 . 2 到其他”手机”,直到它变成“总计”。

标签: c# regex string substring


【解决方案1】:

在 LinqPad 中没有 Regex 的解决方案:

string source = @"1 . 1 To Airtel Mobile
1 03/MAR/2013 16:06:31 9845070641 05:44 1.80 **
2 04/MAR/2013 10:00:29 9845096416 00:14 0.30 **
Total 25:28 9.30
1 . 2 To Other Mobiles
1 03/MAR/2013 06:41:06 9448485859 00:15 0.40 **
2 04/MAR/2013 18:57:47 9448367847 08:33 3.60 **
3 05/MAR/2013 18:57:05 9448485859 00:42 0.40 **
4 05/MAR/2013 20:13:19 9448367847 00:42 0.40 **
Total 34:33 18.00
1 . 3 To Fixed Landline
21 21/MAR/2013 11:59:35 08066000000 09:34 5.00
22 22/MAR/2013 11:31:33 08066000000 15:20 8.00
Total 01:35:23 54.00";

string search1 = "1 . 2 To Other Mobiles";
string search2 = "Total";

var result = source.Split(new string[] { search1 }, StringSplitOptions.None)[1].
                    Split(new string[] { search2 }, StringSplitOptions.None)[0].
                    Dump();

输出:

1 03/MAR/2013 06:41:06 9448485859 00:15 0.40 **
2 04/MAR/2013 18:57:47 9448367847 08:33 3.60 **
3 05/MAR/2013 18:57:05 9448485859 00:42 0.40 **
4 05/MAR/2013 20:13:19 9448367847 00:42 0.40 **

根据您的问题,我推测您不希望搜索项出现在输出中,是吗?

如果您需要重复搜索具有相同开始和结束参数的文本块,您可以将其包装在一个方法中,并让它递归地从拆分索引 [1] 开始。

【讨论】:

  • 是的先生..什么是 Dump() 因为它在我的代码中给出错误..
  • 抱歉。忽略.Dump()。这是 LinqPad 的原生方法,它将当前对象扔到输出窗口中。许多 .Net 开发人员使用 www.linqpad.net 作为代码的暂存器。简而言之:只需将其删除。它没有添加任何功能
【解决方案2】:

您可以使用正则表达式来捕获字符串直到第一个search2

var input = "1 . 2 To Other Mobiles asdf asd fas df asd fas dfas df asd fas df sdaf Total asdfasdfasdfasdfasdfasdf another Total ertyertyertyerty eryertwer";

var sub1 = "1 . 2 To Other Mobiles";
var sub2 = "Total";

var match = Regex.Match(input, sub1 + "(.*?)" + sub2, RegexOptions.IgnoreCase);
if (match.Success)
{
   var m = match.Groups[1].Value;
    Console.WriteLine(sub1 + m);
}

这将输出

1 . 2 To Other Mobiles asdf asd fas df asd fas dfas df asd fas df sdaf 

如果您想要直到最后一个search2,然后删除捕获组中的? 使其变得贪婪。

var input = "1 . 2 To Other Mobiles asdf asd fas df asd fas dfas df asd fas df sdaf Total asdfasdfasdfasdfasdfasdf another Total ertyertyertyerty eryertwer";

var sub1 = "1 . 2 To Other Mobiles";
var sub2 = "Total";

// note the capture group is missing the ?    
var match = Regex.Match(input, sub1 + "(.*)" + sub2, RegexOptions.IgnoreCase);
if (match.Success)
{
   var m = match.Groups[1].Value;
    Console.WriteLine(sub1 + m);
}

这将输出

1 . 2 To Other Mobiles asdf asd fas df asd fas dfas df asd fas df sdaf Total asdfasdfasdfasdfasdfasdf another 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-25
    • 1970-01-01
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多