regex - regexp_replace replaces wrong part of the string -
when running:
select regexp_replace('( (test :name (x) :table (y) )','\s+\:name \(.*?\)',' avner '); i get:
"( (test avner " but if run:
select regexp_replace('( (test :name (x) :table (y) )','\:name \(.*?\)',' avner '); i get:
"( (test avner :table (y) )" why \s+ @ start cause matching till end of string?
the reason (per documentation):
a branch — is, re has no top-level | operator — has same greediness first quantified atom in has greediness attribute.
bold emphasis mine. simplifying question to:
select substring('( (test :name (x) :table (y) )', '\s+\:name \(.*?\)') ,substring('( (test :name (x) :table (y) )', '\:name \(.*?\)') if want second quantifier non-greedy, change first non-greedy well. especially, since doesn't change anything:
select substring('( (test :name (x) :table (y) )', '\s+?:name \(.*?\)') and there no need escape colon (:).
Comments
Post a Comment