2

Can someone explain how at the end of each line I place a pipe with regex?

Using find and replace in notepad ++

I'm trying to place content |

Cheers!

1
  • The $ metachar will always be the end of line / end of string in multi-line mode. It's mode should be an external checkbox selection in the interface, I don't know. But there is a big distinction without this: it won't work for what you need as the dollar sign means 'end of string' outside of multi-line mode. For Perl type engines, setting modes inside the regex will override any externally set modes. Using (?m)$ will make your regex portable.
    – sln
    Oct 7, 2022 at 17:23

3 Answers 3

5

If you just want to add a pipe at the end of each line, simply use this 'find' regex:

$

(The dollar symbol matches the end of a line in regex)

And this 'replace':

|

Make sure you enabled regular expression find.

1

Find what: (.*)

Replace with: $1|

This uses grouping, so $1 is basically saying insert what was found in parentheses, and then add the pipe onto the end. The . will capture any character except certain whitespace, such as newlines which is ideal in this situation. The * means allow 0 or more characters to be captured with the .

This is scaleable, so if you want to capture only certain line, e.g. lines that contain test:

Find what: (.*test.*)

Replace with: $1|

So, if you enter those and then hit "Replace All", you'll have pipes at the end of each line that matches the regular expression.

1
  • Very helpful as this can be used in a much broader way. Feb 20, 2018 at 5:15
0

Changed answer.
I would use this. I works in either single or multi line mode.
I'm not sure what notepad has available (i.e. search/replace, find/replace, etc..)
Look for this: (?=\r\n|\n|\r|$)
and insert (replace) this: |

Just some notes on that complicated subject of what $ metachar really is.
It's not as easy as it should be and the Docs leave a lot to be desired.

Anyway here is my take on it -

 # Regular Expression Docs:
 # Metacharacter  $ 
 # Match the end of the line (or before newline at the end)
 # ** This really is misworded, it really means:
 #
 #    In single line mode,
 #      In two separate matches (in global context) 
 #         Match before and after a newline at the end of a string if there is a newline,
 #      OR, Match the end of the string only.
 #
 #    In multi-line mode,
 #         Match before OR after a newline at the end of a line or string,
 #         but not both.
 #
 # ---------------------------------------------
 # The above explanation is conditional upon the qualifying 
 # subexpressions surrounding the $ metachar
 # ---------------------------------------------

 # /$/g  Single line mode:
 # Matches before newline (if there is one) AND end of string (always this)

 print "=== /\$/g ===============\n";
    $str = "0  ";          $str =~ s/$/|/g;  print "'$str'\n---\n";
    $str = "1  \n";        $str =~ s/$/|/g;  print "'$str'\n---\n";
    $str = "2  \n\n";      $str =~ s/$/|/g;  print "'$str'\n---\n";
    $str = "3  \n\n\n";    $str =~ s/$/|/g;  print "'$str'\n---\n";
    $str = "4  \n\n\n\n";  $str =~ s/$/|/g;  print "'$str'\n\n";

 # /$/mg   Multi-line mode:
 # Matches before each newline (if there is one) OR end of string (not both)

 print "===  /\$/mg ===============\n";
    $str = "0  ";          $str =~ s/$/|/mg;  print "'$str'\n---\n";
    $str = "1  \n";        $str =~ s/$/|/mg;  print "'$str'\n---\n";
    $str = "2  \n\n";      $str =~ s/$/|/mg;  print "'$str'\n---\n";
    $str = "3  \n\n\n";    $str =~ s/$/|/mg;  print "'$str'\n---\n";
    $str = "4  \n\n\n\n";  $str =~ s/$/|/mg;  print "'$str'\n\n";

 # /(?=\r\n|\n|\r|$)/g   Single line mode:
 # Parsing the expression for //m Multi-line mode,
 # Equivalent of /$/m  can now be run in Single line mode:
 # This is What /$/m  probably really is.
 # Matches before each newline (if there is one) OR end of string (not both)

 print "=== /(?=\\r\\n|\\n|\\r|\$)/g ==============\n";
    $str = "0  ";          $str =~ s/(?=\r\n|\n|\r|$)/|/g;  print "'$str'\n---\n";
    $str = "1  \n";        $str =~ s/(?=\r\n|\n|\r|$)/|/g;  print "'$str'\n---\n";
    $str = "2  \n\n";      $str =~ s/(?=\r\n|\n|\r|$)/|/g;  print "'$str'\n---\n";
    $str = "3  \n\n\n";    $str =~ s/(?=\r\n|\n|\r|$)/|/g;  print "'$str'\n---\n";
    $str = "4  \n\n\n\n";  $str =~ s/(?=\r\n|\n|\r|$)/|/g;  print "'$str'\n\n";

 # /(?=\r\n|\n|\r|$)/mg   Multi-line mode:
 # Exact same output.

 print "=== /(?=\\r\\n|\\n|\\r|\$)/mg ==============\n";
    $str = "0  ";          $str =~ s/(?=\r\n|\n|\r|$)/|/mg;  print "'$str'\n---\n";
    $str = "1  \n";        $str =~ s/(?=\r\n|\n|\r|$)/|/mg;  print "'$str'\n---\n";
    $str = "2  \n\n";      $str =~ s/(?=\r\n|\n|\r|$)/|/mg;  print "'$str'\n---\n";
    $str = "3  \n\n\n";    $str =~ s/(?=\r\n|\n|\r|$)/|/mg;  print "'$str'\n---\n";
    $str = "4  \n\n\n\n";  $str =~ s/(?=\r\n|\n|\r|$)/|/mg;  print "'$str'\n\n";

Output >>

 === /$/g ===============
 '0  |'
 ---
 '1  |
 |'
 ---
 '2
 |
 |'
 ---
 '3

 |
 |'
 ---
 '4


 |
 |'

 ===  /$/mg ===============
 '0  |'
 ---
 '1  |
 |'
 ---
 '2  |
 |
 |'
 ---
 '3  |
 |
 |
 |'
 ---
 '4  |
 |
 |
 |
 |'

 === /(?=\r\n|\n|\r|$)/g ==============
 '0  |'
 ---
 '1  |
 |'
 ---
 '2  |
 |
 |'
 ---
 '3  |
 |
 |
 |'
 ---
 '4  |
 |
 |
 |
 |'

 === /(?=\r\n|\n|\r|$)/mg ==============
 '0  |'
 ---
 '1  |
 |'
 ---
 '2  |
 |
 |'
 ---
 '3  |
 |
 |
 |'
 ---
 '4  |
 |
 |
 |
 |'
7
  • It doesn't work if end of line is \r.
    – M42
    Jan 17, 2014 at 16:44
  • Thats true. Should I add an \r?
    – sln
    Jan 17, 2014 at 17:46
  • Yes, that's OK but it's a bit more complicated than @Jerry 's answer.
    – M42
    Jan 17, 2014 at 18:08
  • Changed my answer. And I'm not so sure $ is uncomplicated. What thinks you?
    – sln
    Jan 18, 2014 at 2:43
  • $ is as uncomplicated as it gets. Like most editors, NPP always performs regex matches in multiline mode. You can't even turn it off through the user interface. You can disable it in your regex with (?-m), but you'll probably never need to, since \A and \z also work.
    – Alan M
    Jan 18, 2014 at 4:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .