如何使用正则表达式匹配包含特定单词的行的第 N 个单词 [英] How to match the Nth word of a line containing a specific word using regex

查看:61
本文介绍了如何使用正则表达式匹配包含特定单词的行的第 N 个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用正确的正则表达式来匹配包含特定单词的行的第 N 个单词.

I'm trying to do to get the correct regular expression to match the Nth word of a line containing a specific word.

例如,如果我有这个输入:

For example, if I have this input:

this is the first line - blue
this is the second line - green
this is the third line - red

我想匹配包含单词second"的行的第七个单词并返回green.

I want to match the seventh word of the lines containing the word "second" and return green.

我正在使用 Rubular 来测试正则表达式.

I'm using Rubular to test the regular expression.

我已经尝试过这个正则表达式但没有成功 - 它匹配下一行:

I already tried out this regular expression without success - it is matching the next line:

(.*second.*)(?<data>.*?\s){7}(.*)

另一个示例输入:

this is the Foo line - blue
this is the Bar line - green
this is the Test line - red

我想匹配包含单词red"的行的第四个单词并返回Test.

I want to match the fourth word of the lines containing the word "red" and return Test.

我想匹配的词可以出现在我用来选择行的词的之前之后.

The word I want to match can come either before or after the word I use to select the line.

推荐答案

你可以用这个来匹配包含 second 的行并抓取第 7 个单词:

You can use this to match a line containing second and grab the 7th word:

^(?=.*\bsecond\b)(?:\S+ ){6}(\S+)

确保全局和多行标志处于活动状态.

Make sure that the global and multiline flags are active.

^ 匹配一行的开头.

(?=.*\bsecond\b) 是一个正向的前瞻,以确保在该特定行中存在单词 second.

(?=.*\bsecond\b) is a positive lookahead to make sure there's the word second in that particular line.

(?:\S+ ){6} 匹配 6 个单词.

(\S+) 将获得第 7 个.

regex101 演示

您可以将相同的原则应用于其他要求.

You can apply the same principle with other requirements.

一行包含 red 并得到第 4 个单词...

With a line containing red and getting the 4th word...

^(?=.*\bred\b)(?:\S+ ){3}(\S+)

这篇关于如何使用正则表达式匹配包含特定单词的行的第 N 个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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