使用子组简化正则表达式

时间:2013-03-09 23:58:42

标签: c# .net regex

这可以简化以避免重复吗?最好没有重复的组。

以下是我的正则表达式与声誉的结构化输出:

^(?<Version>(\d+)\.(\d+)\.(\d+))(?!.*[\.-]$)(?:(?<IsReleaseVersion>-)(?:(?:(?<RArea1>[\da-z][\.\da-z-]+)\.(?<RStep1>[\d]+)\.(?<RHash1>[\.\da-z]+)\.(?<RCiId1>\d{1,4}))|(?:(?<RArea2>[\da-z][\.\da-z-]+)\.(?<RStep2>[\d]+)\.(?<RId2>[\.\da-z]+))|(?:(?<RArea3>[\da-z][\.\da-z-]+)\.(?<RStep3>[\d]+))|(?:(?<RArea4>[\da-z][\.\da-z-]+))))?(?:(?<IsBuildVersion>\+)(?:(?:(?<BArea1>[\da-z][\.\da-z-]+)\.(?<BStep1>[\d]+)\.(?<BId2>[\.\da-z]+)\.(?<BCiId2>\d{1,4}))|(?:(?<BArea2>[\da-z][\.\da-z-]+)\.(?<BStep2>[\d]+)\.(?<BId2>[\.\da-z]+))|(?:(?<BArea3>[\da-z][\.\da-z-]+)\.(?<BStep3>[\d]+))|(?:(?<BArea4>[\da-z][\.\da-z-]+))))?$

以下是输入文字:

<Version>: (A)=1.0.1 (B)=1.0.1 (C)=1.0.1 (D)=1.0.1 (E)=1.0.1 (F)=1.0.1 (1): (A)=1 (B)=1 (C)=1 (D)=1 (E)=1 (F)=1 (2): (A)=0 (B)=0 (C)=0 (D)=0 (E)=0 (F)=0 (3): (A)=1 (B)=1 (C)=1 (D)=1 (E)=1 (F)=1 <IsReleaseVersion>: (A)=null (B)=null (C)=null (D)=null (E)=null (F)=- <IsBuildVersion>: (A)=+ (B)=+ (C)=+ (D)=+ (E)=null (F)=null <Area>: (A)=beta (B)=beta (C)=beta (D)=beta (E)=null (F)=beta <Step>: (A)=1 (B)=1 (C)=1 (D)=null (E)=null (F)=1 <Id>: (A)=abcd1234 (B)=abcd1234 (C)=null (D)=null (E)=null (F)=abcd1234 <CiId>: (A)=0001 (B)=null (C)=null (D)=null (E)=null (F)=null 

我希望有人能帮助我!

1 个答案:

答案 0 :(得分:1)

哦,你已经弄得很糟糕了:)我只关注这一部分:

((?:(?:(?<RArea1>[\da-z][\.\da-z-]+)\.(?<RStep1>[\d]+)\.(?<RHash1>[\.\da-z]+)\.(?<RCiId1>\d{1,4}))|
(?:(?<RArea2>[\da-z][\.\da-z-]+)\.(?<RStep2>[\d]+)\.(?<RId2>[\.\da-z]+))|
(?:(?<RArea3>[\da-z][\.\da-z-]+)\.(?<RStep3>[\d]+))|
(?:(?<RArea4>[\da-z][\.\da-z-]+)))

这可以改写为:

(?:(?<Area>[\da-z]+)(?:\.(?<Step>[\d]+)(?:\.(?<Hash>[\da-z]+)(?:\.(?<CiId>\d{1,4}))?)?)?)

简单地嵌套您的条件。

action中查看。