? 的用例:在 tcl regexp 中 [英] use case for ?: in tcl regexp

查看:33
本文介绍了? 的用例:在 tcl regexp 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 tcl regexp 中阅读了 ?: 的文档.这表示它匹配一个表达式而不捕获它.

I read the documentation of ?: in tcl regexp. Which says that it matches an expression without capturing it.

我试过了,效果很好.我的问题是,这个选项的正确用例是什么,因为我们不想使用捕获序列,我们不会在那里放括号.

I tried and it worked fine. My query is, what is the proper use case for this option, as it we do not want to use capture sequence, we won't puts brackets there.

它只是一种替代方式,还是有一些特殊情况,我们应该在哪里使用它?请澄清.

Is it just an alternate way, or have some special condition, where we should use this? Kindly clarify.

推荐答案

简单:您需要将 Regex 中的多个元素分组,但不需要将它们作为捕获组以供参考.

Easy: You need to group several elements in your Regex, but you don't need them as a capturing group for reference.

a+ (b+|c+) OR (a+ b+)|c+

我需要大括号进行分组.但是如果我像这样运行它,引擎将捕获所有这些匹配项.这可能需要大量内存并消耗大量性能.如果我以后不需要捕获组作为参考,我可以使用 ?: 在不影响性能的情况下进行分组:

I need braces for grouping. But if I run it like this the engine will capture all those matches. This may need a lot of memory and cost a lot of performance. If I don't need the capturing groups later for reference, I can use ?: to get grouping without the performance impact:

a+ (?:b+|c+) OR (?:a+ b+)|c+

这篇关于? 的用例:在 tcl regexp 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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