如何将此字符串的数字放在数据库中?

时间:2012-12-04 20:09:45

标签: string list parsing vim split

我想将这些字符串的单行号放在名为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]

但由于><符号,我不知道如何分割这些字符串。
问题是我需要><之间的数字而不是字符串self中的数字。

2 个答案:

答案 0 :(得分:1)

你可以使用像这样的正则表达式来破坏你的输入字符串:

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

它有两个捕获组:

  • 第1组:您要创建的整数序列的下限。
  • 第2组:您要创建的整数序列的上限。

然后,您将遍历每个正则表达式匹配,从匹配为您提供的边界生成数字序列。

我不确定您可以访问的代码环境。所以这是一个C#函数,它从您提供的示例输入字符串中生成所需的输出。

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);
}

答案 1 :(得分:-1)

分析数据时,您将识别结构:

  1. \|
  2. 分隔的范围
  3. .
  4. 分隔的下限和上限
  5. 一个绑定包含前面不感兴趣的东西(\%),一个印记(< / >),一个数字,最后是无趣的东西
  6. 取决于sigil,需要增加/减少数量以获得第一个/最后一个行号
  7. 我会逐步处理数据;每个步骤只是对split()matchstr()matchlist()或其他原始操作的简单调用。

    例如,第一步是

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