Skip to Main Content

Breadcrumb

May 4th

Question and Answer

Chris Saxon

Thanks for the question, Radha Sri Seshu.

Asked: September 01, 2017 - 6:59 am UTC

Last updated: September 01, 2017 - 9:37 am UTC

Version: 11.2

Viewed 1000+ times

You Asked

I am trying to understand the meta character of + symbol in regular expressions. I got a doubt that
SELECT REGEXP_COUNT('RADHA SRI SESHU KOLLA','A') FROM DUAL; Output is 3
SELECT REGEXP_COUNT('RADHA SRI SESHU KOLLA','A+') FROM DUAL; Output is 3

How + makes difference. Can you give me any example?

and Chris said...

The plus symbol means match one or more instances of the character before it. You'll notice a difference when you have a series of As next to each other.

For example:

SELECT REGEXP_COUNT('RADHAAA SRI SESHU KOLLA','A') FROM DUAL; 

REGEXP_COUNT('RADHAAASRISESHUKOLLA','A')  
5 

SELECT REGEXP_COUNT('RADHAAA SRI SESHU KOLLA','A+') FROM DUAL;

REGEXP_COUNT('RADHAAASRISESHUKOLLA','A+')  
3   


The first query only matches the single character A. There are five As in the string, so that's your count.

But the second lumps together any As together. So all those at the end of RADHAAA become one match. So you get three because you have three separate sequences of As.

Is this answer out of date? If it is, please let us know via a Comment

More to Explore

SQL

The Oracle documentation contains a complete SQL reference.