如果 Match 位于字符串的开头,则... in ."Regex" 有条件的

If Match is in the beginning of the String then... in ."Regex" Conditional

)) 大家好,我想创建一个此语法的正则表达式条件 (?(regex)then|else) 请一个正则表达式。 这是我的字符串...

cook, eat, write, play

我目前正在使用这个正则表达式

(, )?write

...并用""代替(没什么,直接删)

为了删除 "write word" 和 "comma and space" 可能会或可能不会出现在它之前。所以应用后我得到这个字符串...

cook, eat, play

但是如果我想使用相同的正则表达式 (, )?cook 删除字符串中的第一个单词 "cook",我会得到这个尴尬的结果...

, eat, write, play

所以,我想创建一个可以检查的 Regex 条件... 如果在字符串的开头找到匹配项,则删除之前的匹配项以及以下 "comma and space" 所以它给出结果...

eat, write, play

如何创建该条件?还是其他更好的正则表达式来做到这一点? 请注意,我使用的宏自动化工具只允许我使用正则表达式替换文本,我没有使用任何可以替换正则表达式的语言进行编程。我只需要一个 Regex 解决方案,我只使用这个 .NET Regex 测试器。 http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx

感谢高级。

我希望它更复杂,但如果你必须有条件,
它的(表达):@"(?(^)write,[ ]|,[ ]write)"

 (?( ^ )              # Conditional, is at BOS ? 
      write, [ ]          # Yes, this form
   |                     # else
      , [ ] write          # No, this form
 )                    # end conditional.

但是,没有条件也是一样的:@"^write,[ ]|,[ ]write"

   ^ write , [ ]
|  
   , [ ] write

但是,使用条件语句可能会更复杂。 @"(?>(^)?)(?:,[ ])?write(?(1),[ ])"

 (?>( ^ )?)           # (1), Optional BOS, with atomic group
 (?: , [ ] )?         # Optional comma space for all 
 write                # 'write'
 (?(1) , [ ] )        # Conditional, if group 1 matched, 
                      # then match the space comma after
                      # otherwise, don't match anything else.