odd regex behavior
Posted: Wed Apr 09, 2025 3:15 pm
I'm running XQuery 3.1 with oXygen 26.1 / Saxon EE 12.3 and getting some surprising behavior when using regular expressions. It seems that the regex engine is treating optional capturing groups as non-capturing groups? Am I missing something?
To reproduce: In either the XPath dialog or the XPath/XQuery Builder dialog or in an XQuery script, with XPath/XQuery version set to 3.1 in either case, do
which returns the expected "1234". But when you make one of the groups optional and try to return it, e.g.
it returns blank. I expected it to return "1234", since a match is present. It strikes me as a bug, but my apologies if I'm just thinking about the regex wrong!
To reproduce: In either the XPath dialog or the XPath/XQuery Builder dialog or in an XQuery script, with XPath/XQuery version set to 3.1 in either case, do
Code: Select all
replace("abc1234-5678abcd", "(.+)(\d{4})(\-\d{4})(.*)", "$2", "i")
Code: Select all
replace("abc1234-5678abcd", "(.+)(\d{4})?(\-\d{4})(.*)", "$2", "i")