Search code examples
regexnotepad++

Regex replace between notepad++


I have a list of values in a csv, which i'm trying to alter slightly using a REGEX search and replace using Notepad++. What I have contains the following examples:

Text,UK_BT.BT1.BT1 1,123
Text,UK_BT.BT11.BT11 1,123
Text,IE_text.text.text,123

What i'm wanting to do, is remove the text between first comma and last period, only where the line contains UK_BT. So the output would be:

Text,BT1 1,123
Text,BT11 1,123
Text,IE_text.text.text,123

Does anyone have any clues? Regex really isnt my strongpoint and generally gets forgotten after using it! Thanks,


Solution

  • Find what: ^(?=.*UK_BT.*)([^,]+,).*\.([^\.]+)$

    Replace with \1\2

    Explanation

    ^             # Beginning of the line.
    (?=.*UK_BT.*) # Must contain 'UK_BT' (positive look ahead, not capturing group, not character consuming).
    (             # Beginning of the first group.
      [^,]+,      # Everything but ',' at least one time (+) followed by ',' (first ',').
    )             # End of the first group.
    .*            # Everything zero or more times.
    \.            # A single '.'.
    (             # Beginning of the second group.
     [^\.]+       # Everything but '.' at least one time (this in combination with '\.' allows to find the last '.').
    )             # End of the second group.
    $             # End of the line
    

    \1\2 allows to point to the first and second captured group.