如何使用正则表达式在最后一次模式出现后捕获字符?

How to capture characters after last pattern occurrence with regex?

我目前正在使用此匹配模式的最后一次出现:

__(?:.(?!__+))+$

模式是“__”

但是,我需要匹配模式后面的字符而不是模式本身。

例如,给定字符串 my__field__name__options

我需要捕获 "options" 但上面的正则表达式捕获了“__options”。

如果没有前瞻性,您可以在 PHP:

中使用此正则表达式
.*__\K.+

RegEx Demo

正则表达式分解:

.*   # match zero or more characters (greedy match)
__   # match "__"
\K   # reset matched info
.+   # match 1 or more characters (greedy)

您可以试试:

.*__(.+)

.* 匹配任何文本,如果有的话
__ 匹配 __ (不贪心)
(.+) 匹配并捕获 () 任何文本

https://regex101.com/r/bus5vi/1