.Net的条件正则表达式,检查if(条件){match pattern} else {match else pattern}

时间:2018-06-11 17:51:33

标签: .net regex

我必须在下面的短信中搜索到期日,其中也包含结算日期。

  

"Bill dated 27-May-18 for your airtel fixedline/broadband ID ##### has been sent at abc@abc.com from ebill@abc.com. Due amount: Rs 2,358.82, due date: 15-Jun-18"

可能有许多不同的短信,他们可能没有在短信中明确提及的收费日期或截止日期。所以以前,我们只是在寻找regex的日期格式,

"((\d{4}|\d{2})-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-(\d{4}|\d{2}))|((\d{4}|\d{2})-\d{2}-(\d{4}|\d{2}))|((\d{4}|\d{2})\/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\/(\d{4}|\d{2}))|((\d{4}|\d{2})\/(\d{3}|\d{2})\/(\d{4}|\d{2}))|([0-3][0-9]((st)|(nd)|(rd)|(th))((\s)?)(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))" 

当只有到期日和没有账单日期时,它运作良好。但在上面的短信中,它总是返回账单日期而不是截止日期。

我想找到一个解决方案,它总是会像上面的正则表达式那样返回我的日期,或者如果有任何截止日期,那么它应该在截止日期之前返回我的日期。

我看到的可能解决方案是

  1. 检查截止日期 - (截止日期:)
  2. 如果发现在截止日期旁边返回日期 - (?< =截止日期:)。*
  3. 其他2未找到然后返回使用上述正则表达式存在的任何日期格式

1 个答案:

答案 0 :(得分:0)

您所要做的就是在due date开始时选择[\S\s]due date 这将从字符串的末尾开始,然后回溯直到找到它为止 然后在它之后匹配日期。

如果找不到,它将匹配在字符串中找到的第一个日期

判断它是否与due date匹配,检查组1是否匹配 如果它为null,则不匹配。

正则表达式:

@"(?:[\S\s]*(due\s+date\s*:)\s*)?((\d{4}|\d{2})-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-(\d{4}|\d{2}))|((\d{4}|\d{2})-\d{2}-(\d{4}|\d{2}))|((\d{4}|\d{2})/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/(\d{4}|\d{2}))|((\d{4}|\d{2})/(\d{3}|\d{2})/(\d{4}|\d{2}))|([0-3][0-9]((st)|(nd)|(rd)|(th))((\s)?)(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))"

可读版本:

    (?:
         [\S\s]*  
         ( due \s+ date \s* : )        # (1)
         \s* 
    )?

    (                             # (2 start)
         ( \d{4} | \d{2} )             # (3)
         -
         (                             # (4 start)
              Jan
           |  Feb
           |  Mar
           |  Apr
           |  May
           |  Jun
           |  Jul
           |  Aug
           |  Sep
           |  Oct
           |  Nov
           |  Dec
         )                             # (4 end)
         -
         ( \d{4} | \d{2} )             # (5)
    )                             # (2 end)
 |  (                             # (6 start)
         ( \d{4} | \d{2} )             # (7)
         - \d{2} -
         ( \d{4} | \d{2} )             # (8)
    )                             # (6 end)
 |  (                             # (9 start)
         ( \d{4} | \d{2} )             # (10)
         /
         (                             # (11 start)
              Jan
           |  Feb
           |  Mar
           |  Apr
           |  May
           |  Jun
           |  Jul
           |  Aug
           |  Sep
           |  Oct
           |  Nov
           |  Dec
         )                             # (11 end)
         /
         ( \d{4} | \d{2} )             # (12)
    )                             # (9 end)
 |  (                             # (13 start)
         ( \d{4} | \d{2} )             # (14)
         /
         ( \d{3} | \d{2} )             # (15)
         /
         ( \d{4} | \d{2} )             # (16)
    )                             # (13 end)
 |  (                             # (17 start)
         [0-3] [0-9] 
         (                             # (18 start)
              ( st )                        # (19)
           |  ( nd )                        # (20)
           |  ( rd )                        # (21)
           |  ( th )                        # (22)
         )                             # (18 end)
         (                             # (23 start)
              ( \s )?                       # (24)
         )                             # (23 end)
         (                             # (25 start)
              Jan
           |  Feb
           |  Mar
           |  Apr
           |  May
           |  Jun
           |  Jul
           |  Aug
           |  Sep
           |  Oct
           |  Nov
           |  Dec
         )                             # (25 end)
    )                             # (17 end)

输出

 **  Grp 0 -  ( pos 0 : len 159 ) 
Bill dated 27-May-18 for your airtel fixedline/broadband ID ##### has been sent at abc@abc.com from ebill@abc.com. Due amount: Rs 2,358.82, due date: 15-Jun-18  
 **  Grp 1 -  ( pos 140 : len 9 ) 
due date:  
 **  Grp 2 -  ( pos 150 : len 9 ) 
15-Jun-18  
 **  Grp 3 -  ( pos 150 : len 2 ) 
15  
 **  Grp 4 -  ( pos 153 : len 3 ) 
Jun  
 **  Grp 5 -  ( pos 157 : len 2 ) 
18  
 **  Grp 6 -  NULL 
 **  Grp 7 -  NULL 
 **  Grp 8 -  NULL 
 **  Grp 9 -  NULL 
 **  Grp 10 -  NULL 
 **  Grp 11 -  NULL 
 **  Grp 12 -  NULL 
 **  Grp 13 -  NULL 
 **  Grp 14 -  NULL 
 **  Grp 15 -  NULL 
 **  Grp 16 -  NULL 
 **  Grp 17 -  NULL 
 **  Grp 18 -  NULL 
 **  Grp 19 -  NULL 
 **  Grp 20 -  NULL 
 **  Grp 21 -  NULL 
 **  Grp 22 -  NULL 
 **  Grp 23 -  NULL 
 **  Grp 24 -  NULL 
 **  Grp 25 -  NULL