[英]C# regex. Optional match after string
I have an input like this test1.test2.part01
which I want to strip away to test1.test2
. 我有一个类似
test1.test2.part01
的输入,我想剥离到test1.test2
。 The only thing i know is that it will end with partxx
and probably a dot before the partxx
. 我唯一知道的是它将以
partxx
结尾,并且可能在partxx
之前有一个点。 However, it will not always be a apart. 但是,这并不总是分开的。 Another example of input might be
testas1.tlp2.asd3.part10
which ofcourse should be stripped to testas1.tlp2.asd3
. 输入的另一个示例可能是
testas1.tlp2.asd3.part10
,当然应该将其剥离到testas1.tlp2.asd3
。
I've made all that, no problem. 我做了所有这些,没问题。 The problem is the dot at the end before
partxx
. 问题是在
partxx
之前partxx
的点。 My regex at the moment is: 目前我的正则表达式是:
(.*)\.?part\d{1,2}
But it will include the dot in the group. 但是它将在组中包括点。 I do not want the dot to be in the group.
我不希望该点出现在组中。 The below works as I want it, given that the dot exists, but it will not always be there.
考虑到该点的存在,以下内容可以按我的需要工作,但它并不总是存在。
(.*)\.part\d{1,2}
How can I exclude the optional dot from the group? 如何从组中排除可选点?
Escape the dot 转义点
(.*)\.?part\d{1,2}
The way you have it, the dot is interpreted as meaning "any character" rather than a literal dot. 用这种方式,点将被解释为“任何字符”,而不是文字点。
Alternatively, 或者,
s/\.part\d\d?$//;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.