我想匹配最长的序列,至少要重复一次。
有:
T_send_ack- $ b
结果应该是: pending-cancel-replace_replaced
<试试这个 (。+)(?=。解析方案* \ 1)
查看这里是Regexr
这将匹配任何字符序列和至少一个字符,稍后在字符串中重复。
您需要存储您的匹配并决定哪一个是最长的。
此解决方案需要您正则表达式的风格来支持反向引用和lookaheads。
它将匹配任何字符序列和至少一个字符。+ 和将它存储在组1中,因为括号内的数字太大d it。下一步是正向前瞻(?=。* \ 1),如果捕获的序列出现在字符串中稍后的某个点上,则为true。
I want to match the longest sequence that is repeating at least once
Having:
T_send_ack-new_amend_pending-cancel-replace_replaced_cancel_pending-cancel-replace_replaced
the result should be: pending-cancel-replace_replaced
解决方案Try this
(.+)(?=.*\1)See it here on Regexr
This will match any character sequence with at least one character, that is repeated later on in the string.
You would need to store your matches and decide which one is the longest afterwards.
This solution requires your regex flavour to support backreferences and lookaheads.
it will match any character sequence with at least one character .+ and store it in the group 1 because of the brackets around it. The next step is the positive lookahead (?=.*\1), it will be true if the captured sequence occurs at a later point again in the string.
更多推荐
定期epxressions匹配最长的重复序列
发布评论