0

i have to lines

 o erl pp ng st i g

  v   a  i     r n 

i want to get

"overlapping string "

please help me

i use notepad++

Ro Yo Mi
  • 14,790
  • 5
  • 35
  • 43
  • 2
    I don't know that you can do this easily with regex. If you simply need to read two strings and merge the results. I would just iterate over the first string (using arrays or whatever you choose) and output the character at that position from the first string, unless it is a space. If it is a space, output the character at that same position from the second string (which may also be a space, but that's ok.) – Jeff Neet Jun 17 '16 at 21:16

2 Answers2

2

Description

This isn't really practical, but here's an interesting way to do it. If you know the length of your first string. And you know that the second string is on the next line you can simply find each space and look ahead x characters to find the associated one. In your sample text, your first line is 18 characters long which means the character right below a space is 18 characters away, so we just need to look ahead that number of characters to find the one in question.

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

Regular expression visualization

If you used $1$2 as your replacement, then the first line would contain either the non-spaces from the first line or the character directly below a space.

Examples

Live Demo

https://regex101.com/r/oT5lZ1/1

Sample Text

o erl pp ng st i g
 v   a  i     r n 

After Replacement

overlapping string
 v   a  i     r n 

Explanation

NODE                     EXPLANATION
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    \S                       non-whitespace (all but \n, \r, \t, \f,
                             and " ")
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
 |                        OR
----------------------------------------------------------------------
  \s                       whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
  (?=                      look ahead to see if there is:
----------------------------------------------------------------------
    .{18}                    any character (18 times)
----------------------------------------------------------------------
    (                        group and capture to \2:
----------------------------------------------------------------------
      .                        any character
----------------------------------------------------------------------
    )                        end of \2
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
Ro Yo Mi
  • 14,790
  • 5
  • 35
  • 43
  • i have a question how you get this image https://www.debuggex.com/i/DhQJfjTlZ61N8q2R.png –  Jun 18 '16 at 04:38
2

As others have said regular expressions do not really fit to the problem. But maybe you can use python instead:

"".join(map(max, zip("o erl pp ng st i g",
                     " v   a  i     r n ")))
jil
  • 2,601
  • 12
  • 14
  • I think Jil's answer will scale better if you have access to python, or a similar approach in any language. – Ro Yo Mi Jun 18 '16 at 01:20