Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Post History

66%
+2 −0
Q&A greedy capture with sed

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 solut...

1 answer  ·  posted 1y ago by Trevor‭  ·  last activity 1y ago by r~~‭

Question sed regex
#1: Initial revision by user avatar Trevor‭ · 2025-05-31T05:49:23Z (over 1 year ago)
greedy capture with sed
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:
```console
$ 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:
```console
$ 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.