正则表达式匹配任何空行或以指定字符开头的任何行 [英] Regular expression to match any empty line or any line starting with a specified character

查看:740
本文介绍了正则表达式匹配任何空行或以指定字符开头的任何行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下输入...

;
; 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).

谢谢。

推荐答案

编辑

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

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|$)

一个小解释:

(?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

这篇关于正则表达式匹配任何空行或以指定字符开头的任何行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆