Matchine the characters at the beginning of a line?



  • 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?



  • @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.



  • 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.



  • @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!



  • @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


Log in to reply