如何提取如下字符串中的第一个字? [英] How to extract the first word that follows a string?

查看:176
本文介绍了如何提取如下字符串中的第一个字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,说我有一个文本文件example.txt文件,上面写着:
我喜欢狗。
我最喜欢的狗是乔治,因为他是我的狗。
乔治是一个很好的狗。

For example, say I have a text file example.txt that reads: I like dogs. My favorite dog is George because he is my dog. George is a nice dog.

现在我如何提取乔治因为它是遵循我最喜欢的狗是第一个字?

Now how do I extract "George" given that it is the first word that follows "My favorite dog is"?

如果有作为一个以上的空间,例如
我最喜欢的狗是乔治.....

What if there as more than one space, e.g. My favorite dog is George .....

有没有一种方法能够可靠地提取单词乔治不分的我最喜欢的狗和乔治的空格数的?

Is there a way to reliably extract the word "George" regardless of the number of spaces between "My favorite dog is" and "George"?

推荐答案

您可以这样做:

cat example.txt | perl -pe 's/My favorite dog is\s+(\w+).*/\1/g'

它输出乔治

这篇关于如何提取如下字符串中的第一个字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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