定期epxressions匹配最长的重复序列

编程入门 行业动态 更新时间:2024-10-17 05:34:51
本文介绍了定期epxressions匹配最长的重复序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想匹配最长的序列,至少要重复一次。

有:

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匹配最长的重复序列

本文发布于:2023-10-26 18:34:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531004.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列   最长   epxressions

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!