[英]Conditional regex for .Net, check if (condition){match pattern}else{match else pattern}
I have to search due date in below sms that also contain billed date. 我必须在还包含计费日期的短信下方搜索到期日期。
"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"
there could be many different sms in which they may not have billed date or due date mentioned explicitly in the sms. 可能有许多不同的短信,其中可能没有短信中明确提到的开票日期或到期日。 So previously, we were just looking for a date format by 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))"
It works well when there is only due date and no bill date. 当只有到期日而没有帐单日期时,它会很好地工作。 But in above sms it always returns bill date instead of due date. 但是在上面的短信中,它总是返回账单日期而不是到期日期。
I want to find a solution that it will always return me date like what above regex do or if there is any due date mentioned then it should return me date next to due date. 我想找到一个解决方案,它总是像上面的正则表达式一样返回我的日期,或者如果提到任何到期日,那么它应该在到期日之后返回我的日期。
possible solution I see is 我看到的可能解决方案是
All you have to do is make due date
optional at the beginning [\\S\\s]due date
. 您所要做的就是在[\\S\\s]due date
的开始due date
选择[\\S\\s]due date
。
This will start at the end of the string then backtrack until it finds it 这将从字符串的末尾开始,然后回溯直到找到它
then match the date right after it. 然后匹配紧随其后的日期。
If it's not found, the first date it finds in the string will be matched. 如果找不到,则将在字符串中找到的第一个日期匹配。
To tell if it matched due date
check if group 1 matched. 要确定它是否与due date
匹配,请检查第1组是否匹配。
If it's null, it wasn't matched. 如果为null,则表示不匹配。
The regex: 正则表达式:
@"(?:[\\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))"
Readable version: 可读版本:
(?:
[\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)
Output 输出量
** 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.