I0_ol
I0_ol

Reputation: 1109

How to formulate a specific regex

I'm trying find a regex to grab gif images posted in a chatroom. Gifs are posted using a colon followed by text and/or numbers to describe the image. The chatroom is set up like this

user1:hello i'm user1 :hi
user2::heythere1 hi user1

The gifs in this example are :hi and :heythere1.

The regex I have so far is grep -oE ':[a-zA-Z0-9]+' But this also returns :hello since every username is also followed by a colon. :hello in this example is not a gif. It is just someone saying hello.

Is there a way to alter this regex so that it only returns :hi and :heythere1?

Upvotes: 2

Views: 49

Answers (1)

codeforester
codeforester

Reputation: 42999

Assuming all lines in your text file begin with a username and a colon, you could do this (I have used the same regex as yours):

cut -f2- -d: file | grep -oE ':[a-zA-Z0-9]+'

Input:

user1:hello i'm user1 :hi :h2
user2::heythere1 hi user1

Output:

:hi
:h2
:heythere1

Upvotes: 3

Related Questions