此正则表达式应该做的技巧,但你总是应该指定你使用的语言/框架,因为并非所有的正则表达式引擎都支持相同的功能。
说明 要捕捉将成为捕获组#3((\d+)
)的数量,其中大多数语言引用作为\ 3
(?:KnownString)([a-zA-Z])(.*?)-(\d+)\#\.[a-zA-Z]+
:
(?: # Opens NCG
KnownString # Literal KnownString
) # Closes NCG
( # Opens CG1
[a-zA-Z] # Character class (any of the characters within)
# Anything between a and z
# Anything between A and Z
) # Closes CG1
( # Opens CG2
.*? # . denotes any single character, except for newline
# * repeats zero or more times
# ? as few times as possible
)- # Closes CG2
# Literal -
( # Opens CG3
\d+ # Token: \d (digit)
# + repeats one or more times
) # Closes CG3
\# # Literal #
\. # Literal .
[a-zA-Z]+ # Character class (any of the characters within)
# Anything between a and z
# Anything between A and Z
# + repeats one or more times
您还没有指定已知的前缀是什么,您应该小心地使用已知字符串(特别是句点,加号,星号,问号和括号)来转义特殊字符。
我投票结束这个问题作为题外话,因为它要求一个现成的解决方案,并显示没有最小的努力。 – Maroun
所以基本上,你需要得到#号前的最后一个整数,我是否正确?你试过什么了?你使用什么语言? –
我曾尝试使用在线工具(如http://www.regexr.com/),但对于初学者来说这太复杂了。我需要在#之前的最后一个整数,但只有在KnownString之后。另外,我需要用包含该整数的新序列替换整个序列。我必须在同一个正则表达式中这样做。我甚至不知道这是否可能 – Don