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 -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -