简体   繁体   English

如何制定特定的正则表达式

[英]How to formulate a specific regex

I'm trying find a regex to grab gif images posted in a chatroom. 我正在尝试找到一个正则表达式来获取张贴在聊天室中的gif图像。 Gifs are posted using a colon followed by text and/or numbers to describe the image. Gif使用冒号张贴,后跟文字和/或数字来描述图像。 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 . 此示例中的gif是:hi: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. 到目前为止,我使用的正则表达式是grep -oE ':[a-zA-Z0-9]+'但这也会返回:hello因为每个用户名后面都带有一个冒号。 :hello in this example is not a gif. :hello在此示例中不是gif。 It is just someone saying hello. 只是有人打招呼。

Is there a way to alter this regex so that it only returns :hi and :heythere1 ? 有没有办法更改此正则表达式,使其仅返回:hi:heythere1

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM