正则表达式检查可以在c#中有组吗? [英] Regex check can have group or not in c#
问题描述
嗨
我有字符串First [A,B] [true]这个正则表达式可以工作
^(小于?FirstPart> +)\ [(小于?二部> +)] \ [(小于?ThirdPart> +)] $
但是字符串可以是First [A,B]。
我怎样才能做到两个串。我是说
为First [A,B] [true] - > First-A,B-true
为First [A,B] - > First-A,B
但字符串可以是首先[A,B]。
我怎样才能做两个字符串。我是说
为First [A,B] [true] - > First-A,B-true
为First [A,B] - > First-A,B
简单:使第三组可选,并将其他两个的长度限制为可能的最小值以获得匹配。
< pre lang =text> ^(?< FirstPart>。+?)\ [(?< SecondPart>。+?)](\ [(?< ThirdPart>。+)])?
[edit]自动添加HTML结束标签......:grrrr:[/ edit]
Hi
I have string "First[A,B][true]" this regex can work
^(?<FirstPart>.+)\[(?<SecondPart>.+)]\[(?<ThirdPart>.+)]$
but string can be "First[A,B]".
How can i do it for two string. I mean
for "First[A,B][true]" -> First-A,B-true
for "First[A,B]"-> First-A,B
but string can be "First[A,B]".
How can i do it for two string. I mean
for "First[A,B][true]" -> First-A,B-true
for "First[A,B]"-> First-A,B
Easy: make the third group optional, and limit the length of the other two to the minimum possible to get a match.
^(?<FirstPart>.+?)\[(?<SecondPart>.+?)](\[(?<ThirdPart>.+)])?
[edit]Automatically added HTML closing tags...:grrrr: [/edit]
这篇关于正则表达式检查可以在c#中有组吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!