正则表达式匹配任何空行或以指定字符开头的任何行

时间:2022-02-24 09:37:53

Given the following input...

鉴于以下输入......

;
; comment
; another comment
;

data
data

I am looking for a regular expression that can be used to strip the blank lines and return only the two lines containing the "data" (but leaving the line breaks intact).

我正在寻找一个正则表达式,可用于去除空行并仅返回包含“数据”的两行(但保持换行完整)。

Thanks.

谢谢。

3 个解决方案

#1


3  

Edit

编辑

Wait, I think I understand what you mean: you only want to preserve the line breaks after your "data" lines. If so, try:

等等,我想我理解你的意思:你只想在你的“数据”行之后保留换行符。如果是这样,请尝试:

(?m)^([ \t]*|;.*)(\r?\n|$)

A small explanation:

一个小解释:

(?m)          # enable multi-line option
^             # match the beginning of a line
(             # start capture group 1
  [ \t]*      #   match any character from the set {' ', '\t'} and repeat it zero or more times
  |           #   OR
  ;           #   match the character ';'
  .*          #   match any character except line breaks and repeat it zero or more times
)             # end capture group 1
(             # start capture group 2
  \r?         #   match the character '\r' and match it once or none at all
  \n          #   match the character '\n'
  |           #   OR
  $           #   match the end of a line
)             # end capture group 2

#2


1  

You can replace ^\s*($|;.*) with an empty string to do that.

您可以用空字符串替换^ \ s *($ |;。*)来执行此操作。

#3


1  

"(^;.*$) | (^[\s\t\r\n]*$)"

should match lines starting with a semi colon or empty lines

应该匹配以半冒号或空行开头的行

#1


3  

Edit

编辑

Wait, I think I understand what you mean: you only want to preserve the line breaks after your "data" lines. If so, try:

等等,我想我理解你的意思:你只想在你的“数据”行之后保留换行符。如果是这样,请尝试:

(?m)^([ \t]*|;.*)(\r?\n|$)

A small explanation:

一个小解释:

(?m)          # enable multi-line option
^             # match the beginning of a line
(             # start capture group 1
  [ \t]*      #   match any character from the set {' ', '\t'} and repeat it zero or more times
  |           #   OR
  ;           #   match the character ';'
  .*          #   match any character except line breaks and repeat it zero or more times
)             # end capture group 1
(             # start capture group 2
  \r?         #   match the character '\r' and match it once or none at all
  \n          #   match the character '\n'
  |           #   OR
  $           #   match the end of a line
)             # end capture group 2

#2


1  

You can replace ^\s*($|;.*) with an empty string to do that.

您可以用空字符串替换^ \ s *($ |;。*)来执行此操作。

#3


1  

"(^;.*$) | (^[\s\t\r\n]*$)"

should match lines starting with a semi colon or empty lines

应该匹配以半冒号或空行开头的行