正则表达式忽略起点和终点后的数据

Regex to ignore data after staring point and endpoint

我们如何使用正则表达式删除或过滤数据以删除介于两者之间的数据?起点是第一个日期(日期可以是动态的,它不是固定的),例如 08/03/2020,终点是 capslock 字符串中的最后 3 个(这也是动态的,但大写字母最多只能包含 3 个字符) ) 例如下面字符串中的 TRU。并且应该忽略或删除之后的所有数据

这是我当前的正则表达式:

Regex.Match(text,"(?<=08/03/2020\s+)[\S\s]*?(?=TRU)").Value.Trim

但它不是动态的。

这将被删除,因为它已经在 08/03/2020 和 TRU 之后。

知道我们如何为此设计正则表达式吗?谢谢你。 #要删除的数据

  Processing
       Co-Applicant
       No inquiry records found."

#字符串

"08/03/2020        NOVUS HOME                  Mortgage Company                                                     TRU
                     MORTGAGE
   07/08/2020        FACTUAL DATA                Mortgage Reporter                                                    XPN
   07/08/2020        FCTUALDATA                                                                                       EFX
   07/08/2020        NOVUS HOME                  Mortgage Company                                                     TRU
                     MORTGAGE
   07/07/2020        CROSSCOUNTRY                Mortgage Loan                                                        TRU
                     MORTGAG
   07/07/2020        FACTUAL DATA                Mortgage Reporter                                                    XPN
   07/07/2020        FCTUALDATA                                                                                       EFX
   05/21/2020        CAP ONE NA                  Bank Credit Card                                                     XPN
   05/21/2020        CAPITAL ONE                 Credit Card                                                          TRU
   05/21/2020        CAPITALONE                  Bank                                                                 EFX
   05/20/2020        CROSSCOUNTRY                Mortgage Loan                                                        TRU
                     MORTGAG
   05/20/2020        FACTUAL DATA                Mortgage Reporter                                                    XPN
   05/20/2020        FCTUALDATA                                                                                       EFX
   05/20/2020        FINGERHUT/WEBBANK           Finance Company                                                      XPN
   05/07/2020        EMS                                                                                              EFX
   05/07/2020        GROW FINANCIAL CREDI        Credit Bureau/Mortgage                                               TRU
                                                 Processing
   Co-Applicant
   No inquiry records found."

#预期输出

   "08/03/2020        NOVUS HOME                  Mortgage Company                                                     TRU
                         MORTGAGE
       07/08/2020        FACTUAL DATA                Mortgage Reporter                                                    XPN
       07/08/2020        FCTUALDATA                                                                                       EFX
       07/08/2020        NOVUS HOME                  Mortgage Company                                                     TRU
                         MORTGAGE
       07/07/2020        CROSSCOUNTRY                Mortgage Loan                                                        TRU
                         MORTGAG
       07/07/2020        FACTUAL DATA                Mortgage Reporter                                                    XPN
       07/07/2020        FCTUALDATA                                                                                       EFX
       05/21/2020        CAP ONE NA                  Bank Credit Card                                                     XPN
       05/21/2020        CAPITAL ONE                 Credit Card                                                          TRU
       05/21/2020        CAPITALONE                  Bank                                                                 EFX
       05/20/2020        CROSSCOUNTRY                Mortgage Loan                                                        TRU
                         MORTGAG
       05/20/2020        FACTUAL DATA                Mortgage Reporter                                                    XPN
       05/20/2020        FCTUALDATA                                                                                       EFX
       05/20/2020        FINGERHUT/WEBBANK           Finance Company                                                      XPN
       05/07/2020        EMS                                                                                              EFX
       05/07/2020        GROW FINANCIAL CREDI        Credit Bureau/Mortgage                                               TRU

你可以使用

(?ms)\A(?:\d{2}/\d{2}/\d{2}(?:\d{2})?|−−DATE−−)\s.*\s\p{Lu}{3}$

regex demo

详情

  • (?ms) - RegexOptions.Multiline^ 匹配行首,$ 匹配行尾位置)和 RegexOptions.Singleline. 现在也匹配换行符)内联修饰符
  • \A - 字符串的开头
  • (?:\d{2}/\d{2}/\d{2}(?:\d{2})?|−−DATE−−) - 两位数字,/,两位数字,/ 和两位或四位数字或 −−DATE−− 字符串
  • \s - 一个空格
  • .* - 任意零个或多个字符,尽可能多
  • \s - 一个空格
  • [A-Z]{3} - 三个大写 ASCII 字母(\p{Lu}{3} 匹配任何语言的三个大写字母)
  • $ - 一行结束。