0

"(" の後にすべてを表示するための私の正規表現:

 echo "apples (orange) (plum)" | sed -re 's/^.+\(//' 

出力: plum)

期待される出力:orange) (plum)

最後の文字ではなく、最初に出現する文字をキャッチするにはどうすればよいですか?

4

2 に答える 2

1

Can't. .* and .+ are always greedy. There are other ways to accomplish this though.

Delete all leading non-('s

$ sed 's/[^(]*(//' <<<'apples (orange) (plum)'
orange) (plum)

Or almost equivalent and not really an improvement would be saving the second part using a group.

$ sed 's/[^(]*(\(.*\)$/\1/' <<<'apples (orange) (plum)'
orange) (plum)
于 2012-06-28T11:35:49.057 に答える
1
echo "apples (orange) (plum)" | sed -re 's/^[^(]+\(//'

. は任意の文字に一致するため、sed は行の最後の括弧を監視します。したがって、.数学(^[^(]+\(数学apples (orange) (。したがって、あなたが提案する[^(]*ように、まったく一致しないように使用する必要があります。(

于 2012-06-28T11:21:23.363 に答える