MS Access VBA RegEx cannot deal with lookarounds: Run-time error ‘5017’: Application-defined or object-defined error. Double early/late match needed

This is a follow-up question on A working RegEx match that begins at the first of two OR-words takes the unwanted last OR-word instead if I place more RegEx before it [duplicate]. Thanks go to the helpful remarks of this user and this user under that question.

I got:

"(?>=SELECTs+|,s*)(.+)s+ASs+Fehler.*?(?<=WHERE|HAVING)(.*)"

Or without any lookarounds:

"(?:=SELECTs+|,s*)(.+?)s+ASs+Fehler.*?((?:WHERE|HAVING).*)"

as the needed RegEx for:

SELECT a, bc d AS Error FROM y WHERE 1=1 HAVING 1=1

to be split into two matching groups:

  1. bc d as the code that builds the column called “Error”. This is dummy code, do not care about the SQL.

  2. Anything after one of the two words “WHERE” or “HAVING”.

Yet, having tested everything with the default language and flavour of www.regex101.com, which is PCRE2 (PHP >=7.3) at the time of writing, it did not work in MS Access VBA, which throws for any pattern with a lookaround:

enter image description here

Microsoft Visual Basic – Run-time error ‘5017’: Application-defined or object-defined error.

With Lookarounds:

regexPattern = "(?>=SELECTs+|,s*)(.+)s+ASs+Fehler.*?(?<=WHERE|HAVING)(.*)" '-> error 5017 pop up
regexPattern = "(?<=WHERE|HAVING)(.*)" '-> error 5017 pop up
regexPattern = "(?>=SELECTs+|,s*)(.+)s+ASs+Fehler.*" '-> error 5017 pop up

Without lookarounds, it seems to work in MS Access:

regexPattern = "(?:=SELECTs+|,s*)(.+?)s+ASs+Fehler.*?((?:WHERE|HAVING).*)" ' -> no error 5017, but no matches either
regexPattern = "((?:WHERE|HAVING).*)" '-> working
regexPattern = "(?:=SELECTs+|,s*)(.+?)s+ASs+Fehler" '-> working

I tested it on a bigger SQL and found no matches again for (?:=SELECTs+|,s*)(.+?)s+ASs+Fehler.*?((?:WHERE|HAVING).*), and no matches in Regex101 either. I need a Regex that finds the two matches as shown above and stops before the order by, and works in MS Access VBA:

SELECT test1.col1, test1.col2, test1.col3, test1.col4, "aaa" AS Error INTO test2
FROM test1
WHERE (((test1.col2)="S") AND ((test1.col4)="bbb")) OR (((test1.col2)="S") AND ((test1.col4)="ccc")) OR (((test1.col2)="S") AND ((test1.col4) Is Null))
HAVING 1=1
ORDER BY test1.col2;

What is the RegEx that works in MS Access VBA

  • to get the two matching groups of above:
    • Error column definition without the AS Error tail,
    • WHERE/HAVING clause, beginning with the earliest of the two,
  • allows one of the two or both matches not to be there at all if the SQL code does not have the needed keywords,
  • does not stop at line breaks,
  • and stops before ORDER BY?

Since it is MS Access VBA, lookaheads and lookbehinds are not allowed, as it seems.

The needed RegEx is:

(?:(?:=SELECTs+|,s*)([^,]+?)s+ASs+Error[sS]*?)?(?:((?:WHERE|HAVING)[sS]*)?)(?:ORDER.*)

enter image description here

See https://regex101.com/r/86VUKs/1

Group 1:

"aaa"

Group 2:

WHERE (((test1.col2)="S") AND ((test1.col4)="bbb")) OR (((test1.col2)="S") AND ((test1.col4)="ccc")) OR (((test1.col2)="S") AND ((test1.col4) Is Null))

The [sS] instead of . is needed to also grab line breaks.

If I change the WHERE to abcd, the HAVING clause found as the next:

enter image description here

And if I change the HAVING to abcd as well, nothing is found and the pattern can handle that:

enter image description here

If I now also change Error to abcd, the pattern does not find any group anymore:

enter image description here

Thus, this works. I then tested it in MS Access where it worked as well.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật