Curly braces in regular expressions, sed
Johnny Rosenberg
gurus.knugum at gmail.com
Tue Jan 6 20:37:06 UTC 2015
2015-01-06 21:25 GMT+01:00 Nathan Dorfman <na at rtfm.net>:
> On Tue, Jan 6, 2015 at 2:49 PM, Johnny Rosenberg <gurus.knugum at gmail.com>
> wrote:
> > So I use the following command:
> > sed -r 's/.*([0-9]{1,2}:[0-9]{2},[0-9]{2}).*/\1/' x
> > [...]
> > Why is one digit missing in the third line?
>
> Simple: because the '*' operator that comes right before it is
> "greedy," meaning it will match as much as possible, including the
> first digit if it exists.
>
I actually thought of that greedyness, but I reversed my thought: Since
it's greedy it should match ”10” rather than ”0” since matching ”10” is
greedier than matching ”0”… I never thought of the ”.*” part of the pattern…
But yes, I see it now, thanks!
>
> If you include the first | character in your pattern, it should work
> for the example you posted. Like this: sed -r
> 's/.*\|([0-9]{1,2}:[0-9]{2},[0-9]{2}).*/\1/' x
>
>
Yes, that works. I was actually going to try the following, but for some
reason I didn't, I think I forgot about it…:
sed -r 's/[^0-9]*([0-9]{1,2}:[0-9]{2},[0-9]{2}).*/\1/' x
That is, look for anything that is NOT a number followed by that number
pattern. I tried that now and it works too.
Thanks!
Kind regards
Johnny Rosenberg
ジョニー・ローゼンバーグ
> -nd.
>
> --
> ubuntu-users mailing list
> ubuntu-users at lists.ubuntu.com
> Modify settings or unsubscribe at:
> https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20150106/b727dfb2/attachment.html>
More information about the ubuntu-users
mailing list