C# 这个正则表达式是什么意思

C# 这个正则表达式是什么意思,c#,regex,C#,Regex,我真正需要知道的是: (?(是什么意思 ?:是什么意思 我想弄清楚的正则表达式是: (注意以下正则表达式中的上述符号) (?(?=and)(and)|(blah))模式的用法类似于if-then-else,如(?(表达式)yes | no) i、 如果和还有其他匹配的废话,e和会匹配 (?:)是一个非捕获组。因此它不会包含在组中或用作反向引用\1 所以 相配 and 1900 blah2000 and 2012 blah2013 注意(都是关于小组的) 同样的事情也可以通过这个正则表达式来实现

我真正需要知道的是:

  • (?(
    是什么意思
  • ?:
    是什么意思
  • 我想弄清楚的正则表达式是:

    (注意以下正则表达式中的上述符号)

    (?(?=and)(and)|(blah))
    模式的用法类似于if-then-else,如
    (?(表达式)yes | no)
    i、 如果
    还有其他匹配的
    废话,e
    会匹配

    (?:)
    是一个非捕获组。因此它不会包含在组中或用作反向引用\1

    所以

    相配

    and 1900
    blah2000
    and 2012
    blah2013
    
    注意(都是关于小组的)

    同样的事情也可以通过这个正则表达式来实现
    (和|废话)(?:[1][9]|[2][0])[0-9][0-9]
    。 这些正则表达式唯一不同的地方是形成的组的数量

    因此,我的正则表达式将形成一个组,其中包含
    等等


    您的正则表达式不会形成任何组。只有当它与
    blah
    匹配时才会形成组。

    ?:
    是非捕获组。
    (?ifthen | else)
    用于构造if,then表达式

    你可以在这里阅读更多关于这些的信息


    以下是一些模式的快速参考:

    .   Any character except newline.
    \.  A period (and so on for \*, \(, \\, etc.)
    ^   The start of the string.
    $   The end of the string.
    \d,\w,\s    A digit, word character [A-Za-z0-9_], or whitespace.
    \D,\W,\S    Anything except a digit, word character, or whitespace.
    [abc]   Character a, b, or c.
    [a-z]   a through z.
    [^abc]  Any character except a, b, or c.
    aa|bb   Either aa or bb.
    ?   Zero or one of the preceding element.
    *   Zero or more of the preceding element.
    +   One or more of the preceding element.
    {n} Exactly n of the preceding element.
    {n,}    n or more of the preceding element.
    {m,n}   Between m and n of the preceding element.
    ??,*?,+?,
    {n}?, etc.  Same as above, but as few as possible.
    (expr)  Capture expr for use with \1, etc.
    (?:expr)    Non-capturing group.
    (?=expr)    Followed by expr.
    (?!expr)    Not followed by expr.
    
    表达式
    (?(?=and)(and)|(blah))
    是一个
    if-else
    表达式:)

    您可以在此处测试reqular表达式:

    是非捕获。它的工作原理与
    (…)
    ,但它不会创建供以后重用的反向引用(
    \1
    等)

    (?(condition)true|else)
    
    是一个尝试匹配<代码>条件
    ;如果成功,它将尝试匹配<代码>真
    ,如果失败,它将尝试匹配<代码>其他

    这是一个罕见的正则表达式构造,因为它没有太多的用例

    (?(?=and )(and )|(blah))
    
    可能被改写为

    (and |blah)
    

    将此作为起点看一看是的,我是。是的,我是。在c#中尝试此字符串,它将接受它。只是提醒未来:在代码中的任何正则表达式上方添加注释(可能有示例)为了避免将来出现这样的问题。正则表达式往往是只写代码,如果你不每天做这些事情,那么很难阅读。@Oliver因为正则表达式不是我的,我应该理解它并对它进行修改,所以我不得不发布它。另外,我也不习惯每天都这样做。@harshit:但请补充你的理解作为一个共通点你能用简单的英语解释一下吗?我的意思是为什么有两个AND?我一直在阅读其他答案中给出的链接。我不理解。
    (?(condition)true|else)
    
    (?(?=and )(and )|(blah))
    
    (and |blah)