0

我想将这些字符串的单行号放在一个名为 SingleLineNrs 的列表中:

\%(\%>1l.\%<4l\|\%>5l.\%<7l\|\%>9l.\%<15l\|\|\%>15l.\%<17l\|\%>17l.\%<19l\|\%>24l.\%<29l\|\%>31l.\%<33l\|\%>33l.\%<35l\)

SingleLineNrs 必须是[2,3,6,10,11,12,13,14,16,18,25,26,27,28,32,34]

但是我不知道如何拆分这些字符串,因为>and<符号。
问题是我需要字符串 self 中的>和而不是数字之间的数字。<

4

2 回答 2

1

You can break your input string using a regex like this one:

>(\d+)\D+(\d+)

It has two capture groups:

  • Group 1: The Lower bound of the integer sequence you want to create.
  • Group 2: The upper bound of the integer sequence you want to create.

You would then loop through every regex match producing sequences of numbers from the bounds the match gives you.

I'm not sure what code environment you have access to. So here is a C# function that produces the output you want from the sample input string you provided.

private static string DecodeSequence(string encodedSequence)
{
    const string SEPARATOR = ",";
    const int GRP_LBOUND = 1, GRP_UBOUND = 2;
    Regex boundPairPattern = new Regex(@">(\d+)\D+(\d+)");
    Match matchBoundPair = boundPairPattern.Match(encodedSequence);

    var decodedSequence = new StringBuilder();
    while (matchBoundPair.Success)
    {
        int lBound = Convert.ToInt32(matchBoundPair.Groups[GRP_LBOUND].Value);
        int uBound = Convert.ToInt32(matchBoundPair.Groups[GRP_UBOUND].Value);
        for (int i = lBound + 1; i < uBound; ++i)
        {
            decodedSequence.Append(i).Append(SEPARATOR);
        }
        matchBoundPair = matchBoundPair.NextMatch();
    }
    if (decodedSequence.Length > 0) decodedSequence.Length -= SEPARATOR.Length;
    return String.Format("[{0}]", decodedSequence);
}
于 2012-12-05T13:11:06.740 回答
-1

当您分析数据时,您会识别出结构:

  1. 范围由\|
  2. 下限和上限由.
  3. 边界由前面无趣的东西 ( \%)、印记 ( </ >)、数字和末尾无趣的东西组成
  4. 根据印记,需要增加/减少数字以获得第一/最后一行编号

我会逐步处理数据;每个步骤只是对split()matchstr()matchlist()或其他原始操作的简单调用。

例如,第一步是

:echo split('\%(\%>1l.\%<4l\|\%>5l.\%<7l\|\%>9l.\%<15l\|\|\%>15l.\%<17l\|\%>17l.\%<19l\|\%>24l.\%<29l\|\%>31l.\%<33l\|\%>33l.\%<35l\)', '\\|')
于 2012-12-06T07:39:52.097 回答