gpt4 book ai didi

regex - 用于匹配后跟特定字符的组的正则表达式

转载 作者:行者123 更新时间:2023-12-01 00:12:52 24 4
gpt4 key购买 nike

所以我需要匹配以下内容:

1.2.
3.4.5.
5.6.7.10
((\d+)\.(\d+)\.((\d+)\.)*)对于第一行会很好,但问题是:可能有很多行:可能是一个或多个。
\n仅当有多于一行时才会出现。

在字符串版本中,我是这样理解的: "1.2.\n3.4.5.\n1.2."
所以我的问题是:如果只有一行, \n不需要在最后,但如果有多行, \n除了最后一行之外,每一行都需要在末尾。

最佳答案

这是我建议的模式:

^\d+(?:\.\d+)*\.?(?:\n\d+(?:\.\d+)*\.?)*$

Demo

以下是对该模式的简要说明:
^                   from the start of the string
\d+ match a number
(?:\.\d+)* followed by dot, and another number, zero or more times
\.? followed by an optional trailing dot
(?:\n followed by a newline
\d+(?:\.\d+)*\.?)* and another path sequence, zero or more times
$ end of the string

关于regex - 用于匹配后跟特定字符的组的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56414773/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com