Agnus Dei (jackal) wrote,
Agnus Dei

Non-Greedy Regular Expression (Remove up to first match character)

For some reason this is always hard for me to remember.

Let's say I have some strings of text where each line begins with "something: data" and I just want to remove everything up to the first colon. I might have colons in my actual data so I can't split on colons.

Here's how:

sed 's/^[^:]*://'

What that says is: Starting at the begining of the line (^), match a character not colon until you hit a colon ([^:]*:), then replace it with nothing.

  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened