在终端中使用RegEx从字符串中提取字符串 [英] Extract string from string using RegEx in the Terminal

查看:79
本文介绍了在终端中使用RegEx从字符串中提取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,如first url, second url, third url,并且想在OS X终端中仅提取单词second之后的url(仅第一次出现).我该怎么办?

I have a string like first url, second url, third url and would like to extract only the url after the word second in the OS X Terminal (only the first occurrence). How can I do it?

我最喜欢的编辑器中,我使用了正则表达式/second (url)/并使用了$1来提取它,我只是不知道如何在终端中进行操作.

In my favorite editor I used the regex /second (url)/ and used $1 to extract it, I just don't know how to do it in the Terminal.

请记住,url是实际的URL,我将使用以下表达式之一对其进行匹配:

Keep in mind that url is an actual url, I'll be using one of these expressions to match it: Regex to match URL

推荐答案

echo 'first url, second url, third url' | sed 's/.*second//'

我误会了.更好:

echo 'first url, second url, third url' | sed 's/.*second \([^ ]*\).*/\1/'

或:

echo 'first url, second url, third url' | perl -nle 'm/second ([^ ]*)/; print $1'

这篇关于在终端中使用RegEx从字符串中提取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆