1

When running:

select regexp_replace('( (test :Name (x) :Table (y) )','\s+\:Name \(.*?\)',' avner ');

I get:

"( (test avner "

But if I run:

select regexp_replace('( (test :Name (x) :Table (y) )','\:Name \(.*?\)',' avner ');

I get:

"( (test  avner  :Table (y) )"

Why is the \s+ at the start cause matching till the end of the string?

6
  • I tested this in Regex101. The first regex with \s+ doesn't match to the end of the string. Have you set any flags? Commented Sep 9, 2014 at 16:59
  • The \s isn't matching till the end, the .*? is Commented Sep 9, 2014 at 17:00
  • @Wolph the "x" is enough for that and then we have the ")". Why should it match all the way? it's lazy... Commented Sep 9, 2014 at 17:02
  • What is your desired result? Commented Sep 10, 2014 at 1:38
  • @Erwin Brandstetter The desired result is replacing the spaces before name as well. Something like: "( (testavner :Table (y) )" Commented Sep 10, 2014 at 7:43

1 Answer 1

1

The reason is (per documentation):

A branch — that is, an RE that has no top-level | operator — has the same greediness as the first quantified atom in it that has a greediness attribute.

Bold emphasis mine. Simplifying your question to:

SELECT substring('( (test :Name (x) :Table (y) )', '\s+\:Name \(.*?\)')
      ,substring('( (test :Name (x) :Table (y) )',    '\:Name \(.*?\)')

If you want the second quantifier to be non-greedy, change the first to non-greedy as well. Especially, since this doesn't change anything:

SELECT substring('( (test :Name (x) :Table (y) )', '\s+?:Name \(.*?\)')

And there is no need to escape the colon (:).

SQL Fiddle.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.