1

I have a working script:

Select col from table where regexp_like (col,'^noun[ |s |es ]| noun[ |s |es ]|noun[ |s |es ]$','i');

Can I optimize my three blocks in REGEXP to a shorter form?

Good:
noun abcd
nouns abcd
abcd noun abcd
abcd nounes abcd
abcd noun

Wrong:
nounse abcd
abcd anouns abcd
abcd inoun
2
  • If you meant to use a group () instead of [] maybe ^ ?noun( |s |es )$ Commented May 19, 2018 at 10:43
  • Unfortunately it does not work Commented May 19, 2018 at 11:05

1 Answer 1

3

In most regex engines a word boundary \b can be used to get the seperate words.
But in Oracle regex you need to do that differently.

(^|\s)noun(e?s)?(\s|$)

(^|\s) : start of string or whitespace
(e?s)? : optional group that has 'es' or 's'
(\s|$) : whitespace or end of string

Setting up test data:

create table test_table (id number(8,0), col varchar2(30), matchexpected char(1));

insert into test_table (id, col, matchexpected) values (1,'noun abcd','Y');
insert into test_table (id, col, matchexpected) values (2,'nouns abcd','Y');
insert into test_table (id, col, matchexpected) values (3,'abcd NOUN abcd','Y');
insert into test_table (id, col, matchexpected) values (4,'abcd nounEs abcd','Y');
insert into test_table (id, col, matchexpected) values (5,'abcd noun','Y');

insert into test_table (id, col, matchexpected) values (6,'nounse abcd','N');
insert into test_table (id, col, matchexpected) values (7,'abcd anouns abcd','N');
insert into test_table (id, col, matchexpected) values (8,'abcd inoun','N');

Example Query:

select * 
from test_table
where regexp_like (col,'(^|\s)noun(e?s)?(\s|$)','i');

Or use \W (non-word character: [^A-Za-z0-9_]) in the regex. Instead of \s (whitespace). To match also strings like "abc nounes!".

select * 
from test_table
where regexp_like (col,'(^|\W)noun(e?s)?(\W|$)','i');

Result:
The first 5 id's.

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

1 Comment

@MarioBernatti Just thought of something. If you also want to match a string like "abcd noun." then the regex should be (^|\W)noun(e?s)?(\W|$). A \W matches a non-word character.

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.