0

i'm looking for most efficient method of lifting text between certain SWIFT tags.

My string contains the following

:59:/SOMETEXT
MORETEXT
EVEN MORE TET
:71A:/some other text

So in the example above, i need to store all text lines between :59: and :70: however, tag :59: can also be represent as follows:

:59a:/SOMETEXT
MORETEXT
EVEN MORE TET
:71A:/some other text

I was thinking of looping through each and saving current tag and previous tag, and then checking if the previous tag was 71A, but there could be further 'Optional' tags in-between tag 59a and tag 71a

Any suggestions on how to handle this?

4

2 回答 2

1
var str = ":59:/SOME TEXT\n" +
          "MORE TEXT\n" +
          "EVEN MORE TEXT\n" +
          ":71A:/some other text\n";

var text = str.Split("\n\r".ToCharArray()).SkipWhile(l => !l.StartsWith(":59")).TakeWhile(l => !l.StartsWith(":71"));

var result = text.Select(l => new string(l.SkipWhile(c => Char.IsPunctuation(c) || Char.IsDigit(c)).ToArray()));

foreach (var l in result)
    Console.WriteLine(l); 

// output: SOME TEXT
//         MORE TEXT
//         EVEN MORE TEXT
于 2014-02-16T12:37:11.997 回答
1

我会使用正则表达式,因为它们比循环更容易维护:

    public string GetText(string text, string tag1, string tag2)
    {
        return Regex.Match(text, String.Format(":{0}[^:]?:(?<text>(\n|.)*):{1}[^:]?:", tag1, tag2)).Groups["text"].Value;
    }
于 2014-02-16T12:15:54.840 回答