• Login
Community
  • Login

Need help for regular expression - find, insert and delete

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
11 Posts 3 Posters 3.1k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V
    Venus642
    last edited by Oct 14, 2020, 12:35 PM

    Hello. I hope for help (absolute newbie).
    I’m trying to change several lines

    Ok I found this regex: ^\d+(-\d+)?\s
    but it delete only numbers and unfortunately it’s not what I’m looking for.

    1. Txt example - rows with numbers:
      ===============
      02 Txt Txt Txt
      220 Txt Txt Txt
      03 | Txt Txt Txt | 2
      130 | Txt Txt Txt | 2
      etc.

    ================

    1. This is OK:
      02 | Txt Txt Txt
      220 | Txt Txt Txt
      03 | Txt Txt Txt
      130 | Txt Txt Txt
      etc.

    The text must be searched. Will not be a | found in the line after the number, it should be set.
    Is a | at the end then delete the last digits (see example 2).

    Big thanks for your help!!

    A 1 Reply Last reply Oct 14, 2020, 12:49 PM Reply Quote 0
    • A
      Alan Kilborn @Venus642
      last edited by Oct 14, 2020, 12:49 PM

      @Venus642

      Perhaps this is a correct clarification:

      • If a | character is not found after the leading digits on a line, insert it with a space before it and a space after it

      • If a | character is found anywhere else in a line, remove it and anything that follows it (and any space before it)

      1 Reply Last reply Reply Quote 0
      • V
        Venus642
        last edited by Oct 14, 2020, 1:12 PM

        Thanks Alan for your Info and help.

        How can I change that with regex?
        From:
        220 Txt Txt Txt
        To:
        220 | Txt Txt Txt

        Or delete and insert:
        From:
        10 Txt Txt Txt | 300
        To:
        10 | Txt Txt Txt

        Sorry. I have no idea how to make regex do it…

        A A 2 Replies Last reply Oct 14, 2020, 1:38 PM Reply Quote 0
        • A
          Alan Kilborn @Venus642
          last edited by Oct 14, 2020, 1:38 PM

          @Venus642

          I would do two separate steps:

          find: ^(\d+ )([^|])
          repl: ${1}| ${2}

          and then:

          find: (?-s)^(\d+ \|.*?) \|.*
          repl: ${1}

          V 1 Reply Last reply Oct 14, 2020, 1:44 PM Reply Quote 3
          • V
            Venus642 @Alan Kilborn
            last edited by Oct 14, 2020, 1:44 PM

            WooooooooW!
            Alan -Big big thanks for your help!

            A 1 Reply Last reply Oct 14, 2020, 1:51 PM Reply Quote 1
            • A
              Alan Kilborn @Venus642
              last edited by Oct 14, 2020, 1:51 PM

              @Venus642 said in Need help for regular expression - find, insert and delete:

              Big big thanks for your help!

              Pay back by learning about how it worked, so that next time you don’t have to ask. :-)

              Start with information HERE

              1 Reply Last reply Reply Quote 0
              • A
                astrosofista @Venus642
                last edited by Oct 14, 2020, 1:51 PM

                Hi @Venus642, @Alan-Kilborn

                Another option, in one step:

                Search: (?-s)(?:^(\d+) *?([A-Za-z]+))|( *?\| *?\d+)$
                Replace: ?1($1 | $2)

                Have fun!

                V 1 Reply Last reply Oct 14, 2020, 1:59 PM Reply Quote 1
                • V
                  Venus642 @astrosofista
                  last edited by Oct 14, 2020, 1:59 PM

                  One step - Perfect…
                  Alan and astrosofist - Top help!

                  A 1 Reply Last reply Oct 14, 2020, 2:12 PM Reply Quote 0
                  • A
                    Alan Kilborn @Venus642
                    last edited by Oct 14, 2020, 2:12 PM

                    @Venus642 said in Need help for regular expression - find, insert and delete:

                    One step - Perfect…

                    Yea, I wouldn’t get too bogged down in it needing to be a one-step operation.
                    If it is something you need to do often, you will make it a macro anyway (so you can give it a meaningful name).
                    And if it is a macro, it can just as well be two replacement steps (because, by the nature of macros, multiple steps look like one step).

                    V 1 Reply Last reply Oct 14, 2020, 2:22 PM Reply Quote 0
                    • V
                      Venus642 @Alan Kilborn
                      last edited by Oct 14, 2020, 2:22 PM

                      @Alan-Kilborn
                      Alan I have tested again and your solutions is better in my case.

                      A 1 Reply Last reply Oct 14, 2020, 2:32 PM Reply Quote 1
                      • A
                        Alan Kilborn @Venus642
                        last edited by Oct 14, 2020, 2:32 PM

                        @Venus642 said in Need help for regular expression - find, insert and delete:

                        Alan … your solutions is better

                        Well, I certainly wasn’t trying to “win”. :-)
                        I often try to point out that a solution to such a problem doesn’t have to be all crammed into one operation.
                        It can make the operation harder to understand for a novice with regular expressions.

                        1 Reply Last reply Reply Quote 0
                        6 out of 11
                        • First post
                          6/11
                          Last post
                        The Community of users of the Notepad++ text editor.
                        Powered by NodeBB | Contributors