我如何在正则表达式中重叠两行?

how i can overlap two lines in regex?

我必须排队

 o erl pp ng st i g

  v   a  i     r n 

我想得到

"overlapping string "

请帮帮我

我用记事本++

描述

这不太实用,但这是一种有趣的方法。如果您知道第一个字符串的长度。而且您知道第二个字符串在下一行,您可以简单地找到每个 space 并向前看 x 个字符以找到相关联的字符串。在您的示例文本中,您的第一行有 18 个字符长,这意味着 space 正下方的字符距离 18 个字符,因此我们只需要向前看那个字符数即可找到有问题的字符。

(\S)|\s(?=.{18}(.))

如果您使用 </code> 作为替换,那么第一行将包含来自第一行的非 space 或 space 正下方的字符。</p> <h1>例子</h1> <p><strong>现场演示</strong></p> <p><a href="https://regex101.com/r/oT5lZ1/1" rel="nofollow">https://regex101.com/r/oT5lZ1/1</a></p> <p><strong>示例文本</strong></p> <pre><code>o erl pp ng st i g v a i r n

替换后

overlapping string
 v   a  i     r n 

说明

NODE                     EXPLANATION
----------------------------------------------------------------------
  (                        group and capture to :
----------------------------------------------------------------------
    \S                       non-whitespace (all but \n, \r, \t, \f,
                             and " ")
----------------------------------------------------------------------
  )                        end of 
----------------------------------------------------------------------
 |                        OR
----------------------------------------------------------------------
  \s                       whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
  (?=                      look ahead to see if there is:
----------------------------------------------------------------------
    .{18}                    any character (18 times)
----------------------------------------------------------------------
    (                        group and capture to :
----------------------------------------------------------------------
      .                        any character
----------------------------------------------------------------------
    )                        end of 
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------

正如其他人所说,正则表达式并不能真正解决问题。但也许你可以使用 python 代替:

"".join(map(max, zip("o erl pp ng st i g",
                     " v   a  i     r n ")))