示例:
aaa.bbbb.ccc4.ddd1.eee.fff
1112.2223.333.4445.555.6661.7773.8881.999以及如何使用一个表达式返回ddd和777,其中它们始终是点之间最后第三个字符串的前3个字符。
我知道如何在两个表达式中这样做:
`[^\.]+\.[^\.]+\.[^\.]+$`
`^\w{3}`有办法把它们结合在一起吗?第二次费用不是适用于原费用,而是适用于第一次费用的结果?
发布于 2020-07-23 21:16:14
发布于 2020-07-23 21:37:26
您可以匹配正则表达式。
(?<=\.).{3}(?=[^.]*(?:\.[^.]*){2}$)regex引擎执行以下操作。
(?<=\.) : positive lookbehind asserts previous
char was '.'
.{3} : match 3 chars
(?= : begin positive lookahead
[^.]* : match 0+ chars other than '.'
(?:\.[^.]*) : match '.' then 0+ chars other than
'.' in a non-capture group
{2} : execute non-capture group twice
$ : assert end of string
) : end positive lookahead另一种方法是使用正则表达式。
(?=\.(.{3})[^.]*(?:\.[^.]*){2}$)捕获捕获组1中所需的3-字符字符串。
(?= : begin positive lookahead
\. : match '.'
(.{3}) : match 3 chars in capture group 1
[^.]* : match 0+ chars other than '.'
(?:\.[^.]*) : match '.' then 0+ chars other than
'.' in a non-capture group
{2} : execute non-capture group twice
$ : assert end of string
) : end positive lookahead如果匹配成功,则匹配字符串开头的空字符串,但感兴趣的是捕获组1的内容。
发布于 2020-07-24 09:33:34
这里是另一种选择:
(?=(\.[^.]*){3}$)\.(.{3})你会匹配的地方:
(?= -正前瞻。(\.[^.]*){3} -第一个捕捉组来匹配一个文字点,除了一个点零或更多次。重复捕获组三次。$) -结束字符串连接和关闭前瞻性.\. -文字点。(.{3}) -第二组捕捉前三位数后的点。从第二组中提取。或者,如果你想要的话,你可以使用一个非纯组并从第一组捕获:(?=(?:\.[^.]*){3}$)\.(.{3})。
https://stackoverflow.com/questions/63063067
复制相似问题