正则表达式忽略捕获的中间部分 [英] Regex ignore middle part of capture

查看:1356
本文介绍了正则表达式忽略捕获的中间部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个单一的正则表达式,当适用于:firstsecondthird将匹配firstthird(单组,也就是说,在C# Match.Value 将等于 firstthird)。

I want a single regex that when applied to : "firstsecondthird" will match "firstthird" (in single group, ie in C# Match.Value will be equal to "firstthird").

这可能吗?我们可以忽略前缀或后缀,但中间?

Is that possible? we can ignore suffix or prefix , but middle?

推荐答案

匹配以'第一'开头的字符串,具有零个或多个其它字符,然后用'第三'结尾。那是什么意思?

match a string that starts with 'first', has zero or more other characters, then ends with 'third'. Is that what you mean?

"^first(.*)third$"

或者,你的意思是,如果你找到一个字符串'firstsecondthird',从'第一'分开抛弃一切,第三?

Or, do you mean if you find a string 'firstsecondthird', ditch everything apart from 'first' and 'third'?

replace("^(first)second(third)$", "$1$2")

这篇关于正则表达式忽略捕获的中间部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆