Regex101 vs Oracle Regex -
my regex:
^\+?(-?)0*([[:digit:]]+,[[:digit:]]+?)0*$ it removing leading + , leading , tailing 0s in decimal number.
i have tested in regex101
for input: +000099,8420000 , substitution \1\2 returns 99,842
i want same result in oracle database 11g:
select regexp_replace('+000099,8420000','^\+?(-?)0*([[:digit:]]+,[[:digit:]]+?)0*$','\1\2') dual; but returns 99,8420000 (tailing 0s still present...)
what i'm missing?
edit
it works greedy quantifier * @ end of regex, not lazy *? set lazy one.
the problem well-known worked henry spencer's regex library implementations: lazy quantifiers should not mixed greedy quantifiers in 1 , same branch since leads undefined behavior. tre regex engine used in r shows same behavior. while may mix lazy , greedy quantifiers extent, must make sure consistent result.
the solution use lazy quantifiers inside capturing group:
select regexp_replace('+000099,8420000', '^\+?(-?)0*([0-9]+?,[0-9]+?)0*$','\1\2') result dual see online demo
the [0-9]+?,[0-9]+? part matches 1 or more digits few times possible followed comma , 1 or more digits, few possible.
some more tests (select regexp_replace('+00009,010020','[0-9]+,[0-9]+?([1-9])','\1') dual yields +20) prove the first quantifier in group sets quantifier greediness type. in case above, group 0 quantifier greediness set greedy first ? quantifier, , group 1 (i.e. ([0-9]+?,[0-9]+?)) greediness type set first +? (which lazy).

Comments
Post a Comment