2

我有一个文件,看起来像这样:

expression1
- expresson1.1
- expressoion1.2
expression2
-expression2.1
expression3
-expression3.1
-expression3.2
-expression3.3

我想要做的是,删除第 1.2、3.2 和 3.3 行,所以只有不以减号开头的行,下一行保留。

所以我尝试编写一个正则表达式,匹配以减号开头的每一行,而前一行也以减号开头,然后删除它们。

到目前为止,没有成功。任何提示将不胜感激。

4

3 回答 3

1

如果您会接受 awk 解决方案,请检查以下单行:

awk '/^[^-]/{f=1;print;next;}{if(f && $0~/^-/){print;f=0;}}' yourFile

测试

kent$  echo "expression1
dquote> - expresson1.1
dquote> - expressoion1.2
dquote> expression2
dquote> -expression2.1
dquote> expression3
dquote> -expression3.1
dquote> -expression3.2
dquote> -expression3.3
dquote> "|awk '/^[^-]/{f=1;print;next;}{if(f && $0~/^-/){print;f=0;}}'
expression1
- expresson1.1
expression2
-expression2.1
expression3
-expression3.1
于 2012-09-26T09:34:52.773 回答
1

You can use this regex:

/(-.+)\s((-.+)?(\s|))+/g

Replacing the matches with:

$1\n

You can see this regex in action with RegExr here.

P.S. tough problem, here lookarounds don't work because you don't have fixed length prefixes to match against, while lookbehind (and lookahead) require fixed length strings as pointed out here (near the end of the page).

于 2012-09-26T10:42:29.133 回答
0

根据您的正则表达式风格,可能有一种方法可以激活多行模式。在这种情况下,除了整个字符串的开头和结尾之外,还匹配行开头和结尾^$

/^-.*$\n((^-.*$\n?)+)/

This should match at least two consecutive lines starting with -. The first capture/subpattern contains all lines after the first one. So these matches would be what you want to delete.

Of course, replace \n with the line ending appropriate for your system.

于 2012-09-26T09:41:36.853 回答