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 (:).

sql fiddle.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

c# - How do I get the Nth largest element from a list with duplicates, using LINQ? -

jsp - "Sending a redirect is forbidden after the response has been committed" in sendRedirect -