简单的东西我想但是我一直坚持什么是正则表达式来匹配在字符串的开头只有一个星号的行而不是前面有1-4的那些? grep'^ *'匹配所有内容
答案 0 :(得分:2)
这个怎么样:
grep '^[*][^*]'
要打破它,
^ <-- Beginning of the line
[*] <-- Exactly one instance of the character *
[^*] <-- Exactly one instance of a character that is NOT *
修改强>
正如Amadan在他的评论中指出的那样,这将错过任何只包含一个*
的行而不是其他任何行,因为在这种情况下[^*]
部分无法匹配。如果您想要考虑以单个*
开头的“空”行,那么您可以将其更改为
grep '^[*]\([^*]\|$\)'
分解为
^ <-- Beginning of the line
[*] <-- Exactly one instance of the character *
\(X\|Y\) <-- Either expression X OR expression Y
[^*] <-- Exactly one instance of a character that is NOT *
-- OR --
$ <-- The end of the line
答案 1 :(得分:2)
试试这个
grep ^\* [^ *] *$
^
行的开头
$
行尾
答案 2 :(得分:1)
你可以试试这个,
创建包含以下内容的文件
cat a.txt
*abc
**def
***gh
*****
grep'^ * [^ *]'a.txt
Ans : *abc
您可以在unix服务器中尝试此操作