-1

I'm using Notepad++ to replace some lines. Basically what I want to do is:

line 1 -

STR::P=FOOXPATTERN=5 AND MORETHINGS YPATTERN=9 BUT XPATTERN=3 AND YPATTERN=20

line 2 -

MOR::P=BAR XPATTERN=1 STRSTR MORETHINGS YPATTERN=1BUT XPATTERN=10 AND YPATTERN=40

...

So this must be transformed in:

line 1

XPATTERN=5|YPATTERN=9|PATTERN=3|YPATTERN=20

line 2 -

XPATTERN=1|YPATTERN=1|XPATTERN=10|YPATTERN=40

My point is that I can have many XPATTERN and many YPATTERN in the same line. Then I would like to replace all my line for the pattern found.

I tried to use negation on regex, but with no success.

Barmar
  • 596,455
  • 48
  • 393
  • 495
Warvimo
  • 35
  • 7
  • Please, [edit your question](https://stackoverflow.com/posts/60012019/edit) and add real values for `XPATTERN` & `YPATTERN` in a real test case. – Toto Feb 01 '20 at 11:13

2 Answers2

0

Use a regexp that matches the pattern and anything before it, and replaces it with just the pattern.

Replace: .*?((XPATTERN|YPATTERN|ZPATTERN|...)=\d+)
With: |\1

If there's something after all the patterns, you can remove the rest after the above replacements with:

Replace: ^((\|(XPATTERN|YPATTERN|ZPATTERN|...)=\d+)*).*
With: \1

This will leave a | at the beginning of each line. You can remove that as a third step:

Replace: ^\|
With: empty string

Barmar
  • 596,455
  • 48
  • 393
  • 495
  • I wrote YPATTERN and XPATTERN for better understanding. Actually in each line I have two different patterns like....**ZBR_4540XX::** and **OOPP400X!!** – Warvimo Jan 31 '20 at 22:33
  • something like.../(xpattern=\d|ypattern=\d*)/ – Warvimo Jan 31 '20 at 22:35
  • WOW. It's almost perfect, thank you! but I forgot to mention that can be more string in the end of the line. I swear that I try to do it, but couldln`t. Can you show me? – Warvimo Jan 31 '20 at 22:41
  • That makes it harder. This would be much easier in a programming language than a text editor. – Barmar Jan 31 '20 at 22:44
0
  • Ctrl+H
  • Find what: (?:^|\G(?!^)).*?((?:XPATTERN|YPATTERN)=\d+)(?:(?!(?:XPATTERN|YPATTERN)=).)*($)?
  • Replace with: $1(?2:|))
  • CHECK Match case
  • CHECK Wrap around
  • CHECK Regular expression
  • UNCHECK . matches newline
  • Replace all

Explanation:

(?:                     # non capture group
    ^                       # beginning of line
  |                       # OR
    \G(?!^)                 # restart from last match position, not at the beginning of line
)                       # end group
.*?                     # 0 or more any character but newline
(                       # group 1
    (?:                     # non capture group
        XPATTERN                # XPATTERN
      |                       # OR
        YPATTERN                # YPATTERN
    )                       # end group
    =\d+                    # equal sign followed by 1 or more digits
)                       # end group 1
(?:                     # non capture group
    (?!                     # negative lookahead, make sure we haven't after:
        (?:                     # non capture group
            XPATTERN                # XPATTERN
          |                       # OR
            YPATTERN                # YPATTERN
        )                       # end group
        =                       # equal sign
    )                       # end lookahead
    .                       # any character but newline
)*                      # end group, may appear 0 or more times
($)?                    # group 1, end of line, optional

Replacement:

$1              # content of group 1 (i.e. X or Y PATTERN = digits)
(?2             # IF group 2 exists (end of line), do nothing
    :           # ELSE
    |           # add a pipe character
)               # ENDIF

Screen capture (before):

enter image description here

Screen capture (after):

enter image description here

Toto
  • 83,193
  • 59
  • 77
  • 109