按分隔符分割而不将其从字符串中移除

我想用正则表达式来分隔长字符串的分隔线。 行可以包含任何可能的unicode字符。 线在点(“。” - 一个或多个)或换行(“ n”)上“结束”。

例:

这个字符串将是输入:

"line1. line2.. line3... line4.... line5..... line6
n
line7"

输出:

  • “一号线”。
  • “2号线。”
  • “3号线......”
  • “4号线......”
  • “LINE5 .....”
  • “LINE6”
  • “line7”

  • 如果我明白你的要求,你可以尝试一下这样的模式:

    (?<=.)(?!.)|n
    

    这会将字符串分割到任何前面有a的位置. 但没有跟着一个.n字符。

    请注意,此图案保留点后面的任何空格,例如:

    var input = @"line1. line2.. line3... line4.... line5..... line6nline7";
    var output = Regex.Split(input, @"(?<=.)(?!.)|n");
    

    产生

    line1. 
     line2.. 
     line3... 
     line4.... 
     line5..... 
     line6 
    line7 
    

    如果你想摆脱空白,只需将其更改为:

    (?<=.)(?!.)s*|n
    

    但是如果你知道这些点总是会跟着空格,那么可以简化为:

    (?<=.)s+|n
    

    尝试这个:

    String result = Regex.Replace(subject, @"""?(w+([.]+)?)(?:[n ]|[""n]$)+", @"""$1""n");
    
    /*
    "line1."
    "line2.."
    "line3..."
    "line4...."
    "line5....."
    "line6"
    "line7"
    */
    

    正则表达式解释

    "?(w+([.]+)?)(?:[n ]|["n]$)+
    
    Match the character “"” literally «"?»
       Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
    Match the regular expression below and capture its match into backreference number 1 «(w+([.]+)?)»
       Match a single character that is a “word character” (letters, digits, and underscores) «w+»
          Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
       Match the regular expression below and capture its match into backreference number 2 «([.]+)?»
          Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
          Match the character “.” «[.]+»
             Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
    Match the regular expression below «(?:[n ]|["n]$)+»
       Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
       Match either the regular expression below (attempting the next alternative only if this one fails) «[n ]»
          Match a single character present in the list below «[n ]»
             A line feed character «n»
             The character “ ” « »
       Or match regular expression number 2 below (the entire group fails if this one fails to match) «["n]$»
          Match a single character present in the list below «["n]»
             The character “"” «"»
             A line feed character «n»
          Assert position at the end of the string (or before the line break at the end of the string, if any) «$»
    

    如果你想保持所有的点完好无损,点后面会有一个空白空间,那么这可能是你的正则表达式:

    String result = Regex.Replace(t, @".s", @".n");
    

    这将是一个字符串。 你还没有说明你是否想要更多的字符串或结果。

    链接地址: http://www.djcxy.com/p/13467.html

    上一篇: Split by delimiter without remove it from string

    下一篇: Remove a newline before a regex