Community
    • 登入

    Matchine the characters at the beginning of a line?

    已排程 已置頂 已鎖定 已移動 Help wanted · · · – – – · · ·
    5 貼文 3 Posters 437 瀏覽
    正在載入更多貼文
    • 從舊到新
    • 從新到舊
    • 最多點贊
    回覆
    • 在新貼文中回覆
    登入後回覆
    此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
    • Mark GrayM
      Mark Gray
      最後由 編輯

      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 條回覆 最後回覆 回覆 引用 0
      • Alan KilbornA
        Alan Kilborn @Mark Gray
        最後由 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 條回覆 最後回覆 回覆 引用 0
        • PeterJonesP
          PeterJones @Mark Gray
          最後由 編輯

          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 條回覆 最後回覆 回覆 引用 2
          • Mark GrayM
            Mark Gray @PeterJones
            最後由 編輯

            @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 條回覆 最後回覆 回覆 引用 0
            • PeterJonesP
              PeterJones @Mark Gray
              最後由 編輯

              @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 條回覆 最後回覆 回覆 引用 1
              • 第一個貼文
                最後的貼文
              The Community of users of the Notepad++ text editor.
              Powered by NodeBB | Contributors