Community
    • Login

    Hello-Help with regular expressions

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    regex
    3 Posts 3 Posters 1.8k 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.
    • Antonio MiBoostA
      Antonio MiBoost
      last edited by

      Hello there, ive been messing around with regular expressions feature of search and replace and i have to say im not a programmer. I feel im asking a complex thing tho :)

      Have a list in a txt file formatted as above

      123:abc:1.2.3.4.5.2
      def:456:6.7.8.9.6.3

      is it possible to make them like

      1.2.3.4.5.2: 123:abc
      6.7.8.9.6.3: def:456

      evidencing that what is before and after the first and second “:” can be words or numbers | what is among commas after second “:” (1.2.3.4.5) are only numbers.
      I hope i’ve explained correctly what i intend to do :)
      Thanks in advance!

      Scott SumnerS 1 Reply Last reply Reply Quote 0
      • Scott SumnerS
        Scott Sumner @Antonio MiBoost
        last edited by

        @Antonio-MiBoost

        Not so complex, hopefully there is a learning opportunity for you here! Nothing is worse than someone that repeatedly posts about regular expressions, and demonstrates with the questions that they aren’t learning, but are rather just looking for someone to write their regular expression searches for them. :-D

        Find what: ^([^:]+:[^:]+):([^\r\n]+)
        Repl with: \2:\x20\1
        Search mode: Regular expression

        Here’s what it does:

        Find the start of a line, followed by one or more characters that are not : (and remember that as group #1), followed by a :, followed by one or more characters that are not : (and remember that as group #2, followed by a :, followed by one or more characters out to the end of the line.

        Replace the found text with the contents of group #2, a :, a blank space (the \x20) and the contents of group #1.

        1 Reply Last reply Reply Quote 0
        • guy038G
          guy038
          last edited by guy038

          Hello, Antonio,

          If you are sure that the last colon, of each line of your file, is the exact location which separates the two parts, of each line, to be reversed , an other formulation could be :

          SEARCH : (?-s)^(.+):(.+)

          REPLACE : \2: \1 ( with a space between the colon : and the back-reference \1)

          Notes :

          • The (?-s) first part ensures that the dot special character ( . ) represents a single standard character and not any End of Line character

          • Then the part (.+):, look, from beginning of line ( ^ ), for the longest non-empty range of standard characters, followed by the last colon of each line, which is stored as group 1, due to round brackets

          • Finally, the part (.+), grabs all the remaining standard characters of the current line ( = all text after the last colon character ) and stores them in group 2

          Best Regards,

          guy038

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