Search code examples
regexnotepad++

Regex to handle leading spaces (notepad++)


I have a line in a file opened in notepad++

  0 <? 0.00 - (multi) Salt

Note: There are 2 leading spaces at the start of the line.

Using regex replacement in only one go, I have to change the above line to :

0,0.00,multi,Salt   

I have tried is : replace

  ( \d*) <\? \d.\d\d - \(multi\) *

with

\1,\2,multi,

It works fine. Only problem is it adds a leading space at every line in notepad++

How can I modify my regex to not add leading space at the start of every line?

Note L I cannot use the option of "Trim Leading space in notepad".

Can someone please help me on this?

Thanks in advance!

Note: My file has more lines like

  0 <? 0.00 - (multi) Salt  
  1 <? 0.00 - (multi) Vinegar   
  2 <? 0.00 - (multi) Salt and Vinegar  
  3 <? 0.00 - (multi) BBQ Sauce 
  4 <? 0.00 - (multi) Chilli Sauce  
  5 <? 0.00 - (multi) Hawaiian Sauce    
  6 <? 0.00 - (multi) Brown Sauce   
  7 <? 0.00 - (multi) Garlic Mayo and Herb Sauce    
  8 <? 0.00 - (multi) Mayo  
  9 <? 0.00 - (multi) Salad 
  10 <? 0.00 - (multi) Lettuce  
  11 <? 0.00 - (multi) Cabbage  

Tried suggestions below


Solution

  • To use a regex-based replacement in Notepad++, turn on the Regular expression radio button at the bottom of the Find and Replace window! (see my settings below)

    Here is a regex that must work for you:

    ^\h{2}(\d*)\h*<\?\h*(\d\.\d{2})\h*-\h*\(multi\)\h*
    

    Replace with \1,\2,multi,

    The \h is a horizontal whitespace (to exclude newlines). Two occurrences can be matched with {2} limiting quantifier (no need repeating the subpattern).

    See the settings:

    enter image description here

    NOTE: If you have more than 1 leading spaces, change \h{2} to \h{2,}:

    ^\h{2,}(\d*)\h*<\?\h*(\d\.\d{2})\h*-\h*\(multi\)\h*