正则表达式匹配太多 [英] Regex matching too much

查看:225
本文介绍了正则表达式匹配太多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<预类=郎无prettyprint-覆盖> (\ [(C | C)=)?(#([A-FA-F0-9] {1,2}){ 3})\(*)\ [/(C |。C)\]

我想这个表达式匹配类似的文字:这是[C = FFFFFF]白色文字[/ C]和[C =#000]黑色文本[/ C]。



它做匹配只身一BB代码,但如果有后,彼此之间多(比如上例中),它会创建两个BB-代码序列的比赛(1场)。 (从[C = FFFFFF] WH ...到... CK文字[/ C])



这是怎么回事?此外,如何让我的点(。)包括换行符在C#中?


解决方案

如果你不关心嵌套的标签,你可以这样做:



<预类=郎无prettyprint-覆盖> (\ [[CC] =)(#(? [A-FA-F0-9] {3}){1,2})\(*)\ [/ [CC] \]
// ^ - ?懒惰​​匹配

如果你想处理嵌套标签与正则表达式,检查的代码项目本文


(\[(c|C)=)(#?([a-fA-F0-9]{1,2}){3})\](.*)\[/(c|C)\]

I want this expression to match text like: "This is [c=FFFFFF]white text[/c] and [C=#000]black text[/C]."

It do match one BB-code alone, but if there are more after each other (like in the example), it will create a match (1 match) of both BB-code-sequences. (from [c=FFFFFF]wh... to ...ck text[/C])

Why is this happening? Also, how do I make the dot (.) include newlines in C#?

解决方案

If you don't care about nested tags, you can do that :

(\[[cC]=)(#?([a-fA-F0-9]{3}){1,2})\](.*?)\[/[cC]\]
//                                     ^- lazy match

If you want to handle nested tags with regex, check this article on code project.

这篇关于正则表达式匹配太多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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