行尾-添加管道|

行尾-添加管道|

有人可以解释一下如何在每行末尾放置一个带有正则表达式的管道吗?

在记事本++中使用查找和替换

我正在尝试放置content |

干杯!

答案1

如果您只想在每行末尾添加一个管道,只需使用这个“查找”正则表达式:

$

(美元符号在正则表达式中匹配行的末尾)

还有这个‘替换’:

|

确保您启用了正则表达式查找。

答案2

找什么:(.*)

用。。。来代替:$1|

这使用分组,所以$1基本上就是说插入在括号中找到的内容,然后将管道添加到末尾。将.捕获除某些空格之外的任何字符,例如换行符,这在这种情况下是理想的。这*意味着允许使用.

这是可扩展的,因此如果您只想捕获某些行,例如包含测试的行:

找什么:(.*test.*)

用。。。来代替:$1|

因此,如果您输入这些内容然后点击“全部替换”,那么与正则表达式匹配的每一行末尾都会出现竖线。

答案3

更改答案。
我会用这个。我可以在单行或多行模式下工作。
我不确定记事本有什么功能(即搜索/替换、查找/替换等)。
查找这个: (?=\r\n|\n|\r|$)
并插入(替换)这个: |

只是对 $ metachar 到底是什么这个复杂主题的一些说明。
它并不像它应该的那样简单,而且文档还有很多不足之处。

无论如何,这是我的看法 -

 # 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";

输出 >>

 === /$/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  |
 |
 |
 |
 |'

相关内容