简体   繁体   English

查找模式的最后一次出现

[英]Find the last occurrence of a pattern

I'm trying to match the last occurrence of a pattern in a string. 我正在尝试匹配字符串中最后一次出现的模式。

I want to get the last word in the parenthesis in the following string: 我想在下面的字符串中得到括号中的最后一个字:

(Don't match this) and not this (but this) (不要与此匹配)而不是这个(但这个)

I've tried the following, 我试过以下,

\s(\((.*?)\))(?!\))

But this matches both occurrences, not only the last one. 但这与两次事件相匹配,而不仅仅是最后一次。 Is it possible to just match the last one? 是否可以匹配最后一个?

Match all strings in bracket /\\(.*?\\)/g and post process the result 匹配括号/\\(.*?\\)/g所有字符串并对结果进行后处理

You can just match all strings satisfying the pattern, and pick the last element from the resulting array. 您可以匹配满足模式的所有字符串,并从结果数组中选择最后一个元素。 There is no need to come up with a complicated regex for this problem. 没有必要为这个问题提出复杂的正则表达式。

> "(Don't match this) and not this (but this)".match(/\(.*?\)/g).pop()
< "(but this)"

> "(Don't match this) and not this (but this) (more)".match(/\(.*?\)/g).pop()
< "(more)"

> "(Don't match this) and not this (but this) (more) the end".match(/\(.*?\)/g).pop()
< "(more)"

Don't want the () in the result? 不希望结果中的() Just use slice(1, -1) to get rid of them, since the pattern fixes their positions: 只需使用slice(1, -1)来摆脱它们,因为模式修复了它们的位置:

> "(Don't match this) and not this (but this)".match(/\(.*?\)/g).pop().slice(1, -1)
< "but this"

> "(Don't match this) and not this (but this) (more) the end".match(/\(.*?\)/g).pop().slice(1, -1)
< "more"

Using .* to search for the last instance of the pattern 使用.*搜索模式的最后一个实例

This is an alternate solution with simple regex. 这是一个简单的正则表达式的替代解决方案。 We make use of the greedy property of .* to search for the furthest instance matching pattern \\((.*?)\\) , where the result is captured into capturing group 1: 我们利用.*的贪婪属性来搜索最匹配的模式匹配\\((.*?)\\) ,其中结果被捕获到捕获组1中:

/^.*\((.*?)\)/

Note that no global flag is used here. 请注意,此处不使用全局标志。 When the regex is non-global (find first match only), match function returns the text captured by capturing group, along with the main match. 当正则表达式是非全局的(仅查找第一个匹配项)时, match函数返回捕获组捕获的文本以及主匹配。

> "(Don't match this) and not this (but this)".match(/^.*\((.*?)\)/)[1]
< "but this"

> "(Don't match this) and not this (but this) (more) the end".match(/^.*\((.*?)\)/)[1]
< "more"

The ^ is an optimization to prevent the engine from "bumping along" to search at subsequent indices when the pattern .*\\((.*?)\\) fails to match from index 0. 当模式.*\\((.*?)\\)无法与索引0匹配时, ^是一种优化,以防止引擎“碰撞”以搜索后续索引。

You can use non capturing parenthesis to consume previous matches: 您可以使用非捕获括号来使用以前的匹配:

var string="(Don't match this) and not this (but this) definitely not this";
var last_match=string.match(/(?:.*\()([^\)]*)(?:\)[^\(]*)/)[1];

Tested using web developer console: 使用Web开发人员控制台测试:

< var string="(Don't match this) and not this (but this) definitely not this";
< string.match(/(?:.*\()([^\)]*)(?:\)[^\(]*)/)[1]
>"but this"

Here is the link for testing: https://regex101.com/r/gT5lT5/2 以下是测试链接: https//regex101.com/r/gT5lT5/2
If you want that the enclosing parenthesis to be part of the match, check https://regex101.com/r/gT5lT5/1 如果您希望封闭括号成为匹配项的一部分,请查看https://regex101.com/r/gT5lT5/1

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

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