不管大小,提取最后 2 个字段

extract the last 2 fields regardless of size

我一直在尝试获取以下字符串的最后一个 "two fields":

cc-api-data.bar.bar
external-atl3-1.xx.fbcdn.net
fbcdn.net

对于前两个字符串,我只想得到 "bar.bar" 和 "fbcdn.net." 但是,对于最后一个字符串,我希望它与整个字符串匹配,因为它拥有我想要的一切.

我非常有信心我可以用一个简单的脚本来做到这一点,但我在这种情况下尝试使用正则表达式。我只能在最后一个字符串上获取字符串的最后一部分,而不是整个字符串。而且我无法告诉正则表达式采用哪个字段。 我真的只想要最后两个字段,不管有多少定界符。

有什么建议或者有没有可能

我的猜测是,我们可能需要一个表达式,它有一个 $ 锚点,类似于:

([^.]*)\.([^.]*)$

朝向字符串的右侧。

Please see the demo for additional explanation

I was wondering how does regex know to get only the part before the last period. Is it because it grabs any character thats not a "." and because it is at the end of the line? why couldnt it grab the first octet?

好问题,也有点难以解释,通过播放这个演示,我们可以看到在开始比赛之前的许多步骤:

Steps of Regular Expression

它将一个字符一个字符地开始,并根据我们的表达式对其进行测试,它将通过表达式中的规则,但在早期字符或八位字节中,一旦它命中$ 结束锚点,那些早期的字符或八位字节 会失败,因为我们最后的字符串结束规则 已被打破。