在终端中使用RegEx从字符串中提取字符串

在终端中使用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'