Community
    • Login

    Matchine the characters at the beginning of a line?

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    5 Posts 3 Posters 347 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.
    • Mark GrayM
      Mark Gray
      last edited by

      I am trying to sort out a big text file with some errors. Some of the lines are actually on one line (see below)

      ITEMNO1 random other stuff more dataITEMNO1 random other stuff more dataITEMNO1 random other stuff more data
      ITEMNO2 random other stuff more data
      ITEMNO3 random other stuff more dataITEMNO3 random other stuff more data
      

      The “ITEMNO1” in the example above changes from line to line but it is always a six digit number.

      I’d like to add a line break before each occurrence of whatever the first six characters of the line happen to be. Like this

      ITEMNO1 random other stuff more data
      ITEMNO1 random other stuff more data
      ITEMNO1 random other stuff more data
      ITEMNO2 random other stuff more data
      ITEMNO3 random other stuff more data
      ITEMNO3 random other stuff more data
      

      Is there a way to do this in notepad++ or regex?

      Alan KilbornA PeterJonesP 2 Replies Last reply Reply Quote 0
      • Alan KilbornA
        Alan Kilborn @Mark Gray
        last edited by Alan Kilborn

        @Mark-Gray said in Matchine the characters at the beginning of a line?:

        The “ITEMNO1” in the example above changes from line to line but it is always a six digit number.

        I could give you an answer, but I’ll first ask you to show me where a six-digit number is in your data…because I don’t see six consecutive digits in your data.
        If you are going to make data up and talk about “six digits”, I want to see six actual digits in the made-up data.
        To get good help you have to ask a good question.

        1 Reply Last reply Reply Quote 0
        • PeterJonesP
          PeterJones @Mark Gray
          last edited by

          Thank you for before and after data

          FIND = (?-s)^(.{6}).*?(?=\1)
          REPLACE = $0\r\n
          MODE = regular expression

          That puts the first 6 chars in group#1, then allows anything up to but not including another copy of that group; the (?=\1) does a “lookahead” so it’s not grabbing those characters, but they must be what comes next. The $0 in the replace says “keep everything that was matched”, and the \r\n adds a new CRLF newline between the matched text and the next instance of group 1
          When you FIND NEXT, it will see the ITEMN01 as the first 6 chars of the next line, so it repeats. So using your three-line data as the input, I get the 6line output.

          Mark GrayM 1 Reply Last reply Reply Quote 2
          • Mark GrayM
            Mark Gray @PeterJones
            last edited by

            @PeterJones said in Matchine the characters at the beginning of a line?:

            $0\r\n

            Thank you so much! I’m new to using regular expressions and this helps me soo much!

            PeterJonesP 1 Reply Last reply Reply Quote 0
            • PeterJonesP
              PeterJones @Mark Gray
              last edited by

              @Mark-Gray ,

              I’m new to using regular expressions

              For your reference, the official Notepad++ documentation site has a large page devoted to searching, most of which is about regular expressions: https://npp-user-manual.org/docs/searching/

              And this Forum has a regular expression FAQ

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