Comments on greedy capture with sed
Post
greedy capture with sed
+2
−0
I am trying to greedily capture text with sed.
For example, I have the string abbbc, and I want to capture all of the repeated b characters, so that my result is bbb.
Here's an attempt at a solution:
$ sed -n 's/.*\(b\+\).*/\1/p' <<< abbbc
b
As shown in the output of the command, the capture only obtains a single b rather than my desired result bbb.
I know I could prepend and append the "not b" pattern ([^b]) to my capture, which would give me the desired result:
$ sed -n 's/.*[^b]\(b\+\)[^b].*/\1/p' <<< abbbc
bbb
However, this solution is a bit inelegant, and may become much more complicated when the match is not as simple. So I'm hoping there's another way to force the capture to be greedy.

1 comment thread