Skip to Main Content
  • Questions
  • Regular Expression is not working if the search criteria with LIKE and NOT LIKE in single input field

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Yassin.

Asked: May 25, 2018 - 10:09 am UTC

Last updated: June 05, 2018 - 1:05 pm UTC

Version: 12C

Viewed 1000+ times

You Asked

Hi Tom,

Need your help !

Please find the LiveSQL link

Thanks in Advance !

I have a table st_exp with s_desc column only, user has option to search by s_desc criteria.

Scenario :The user may enter text critiria LIKE and NOT LIKE in the same single input field.

Am using REGEXP_LIKE for searching from the query.

sample data below : (contains all records)

select * from test_exp

S_DESC
------
NOK D8StorePQRST
NOK 8.0.1 D81SupiriorCoachStorePUJI
NOK  D81CoachStorePUJI
NOK  D81TesterCoachStorePUJI
NOK  D81RacketPUJI
D81QACoachStorePUJI
NOK D81QAPatchPUJI


scenario 1:
the user want to retrieve records which contains "D8" and "Store"

query 1: (expected result)
select s_desc from test_exp where REGEXP_LIKE(s_desc,'[D81]*(Store)')

S_DESC
-------
NOK D8StorePQRST
NOK 8.0.1 D81SupiriorCoachStorePUJI
NOK  D81CoachStorePUJI
NOK  D81TesterCoachStorePUJI
D81QACoachStorePUJI


scenario 2:
the user want to retrieve records which contains(LIKE) 'D8' and 'Store' but not contain (NOT LIKE) 'Tester' and 'QA'

query 2: (Not expected result)
select s_desc from test_exp where REGEXP_LIKE(s_desc,'[D81][^(Tester|QA)]*(Store)')

S_DESC
--------
NOK D8StorePQRST
NOK  D81CoachStorePUJI


expected 3 records but result shows only 2 records.
But the value "NOK 8.0.1 D81SupiriorCoachStorePUJI" is not retrieved by the query.

definition :
[^] Used to specify a nonmatching list where you are trying to match any character except for the ones in the list.

Please guide me for further accurate results.

Thanks & Regards
Khan
India


with LiveSQL Test Case:

and Chris said...

The regular expression is not doing what you think it's doing...

[^(Tester|QA)]*


Means match one character not in the list zero or more times. NOT zero or more instances of the exact strings "Tester" or "QA".

Similarly:

[D81]


Means match D or 8 or 1.

So with:

NOK 8.0.1 D81SupiriorCoachStorePUJI


You have:

Start the match on the first 8 (5th character, the one at "NOK 8").

Then match any character NOT in the list

(Testr|QA)

This takes you up to:

8.0.1 D81Supi

"r" is in the list. So the regex moves to look for the next group, the string "Store".

"r" <> "Store" => no match!

Any particular reason you need regular expressions instead of standard (not) like?

select s_desc from test_exp 
where  s_desc like '%D8%Store%'
and    s_desc not like '%Tester%'
and    s_desc not like '%QA%';

S_DESC                                
NOK 8.0.1 D81SupiriorCoachStorePUJI   
NOK  D81CoachStorePUJI                
NOK D8StorePQRST  


Rating

  (4 ratings)

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

Comments

Performance

Gh, May 25, 2018 - 4:54 pm UTC

Why not using virtual columns
Add c1 as instr(desc, 'Store')
Add as you need as strings
Put index on those cols

Answer taken me one step ahead- thanks

Md Yassin Khan, May 26, 2018 - 12:36 pm UTC

Very much thanks for the very earliest reply.

Really it is great platform for the professional who stucks with problems.

Very much appreciated keep going...

Thanks
Yassin Khan
Connor McDonald
May 28, 2018 - 1:41 am UTC

glad we could help

Need an recommended solution

Yassin Khan, May 28, 2018 - 4:17 am UTC

Hey Tom/Chris Saxon,

you asked me "Any particular reason you need regular expressions instead of standard (not) like? "

Yes!
because the query contains billions of records in which user can search by description(s_desc).

Requirement is - the user is searching the description LIKE and NOT LIKE with SINGLE INPUT field(multiple LIKES and multiple NOT LIKES) as mention in my sample data.

I can achieve only partially with REGEXP_LIKE as mention in my exapmle.

Please suggest me the best and recommended option/alternative solution to resolve the complete issue and achieve the customer requirements.

Thanks in advance and expecting your perfect and recommended solution.

Regards
Khan
Chris Saxon
June 04, 2018 - 4:13 pm UTC

Why does there being billions of rows matter?

And what exactly are your users entering? Please tell me you're not expecting them to write regular expressions...

Details explaination

Yassin Khan, June 05, 2018 - 5:48 am UTC

your question ?
And what exactly are your users entering? Please tell me you're not expecting them to write regular expressions...

Below is real time scenario :

In application we have given two option either (Simple search OR Regular expression search).

if the user select Simple search option LIKE will work perfectly and the user will enter only LIKE input parameter.

But if the user select Regular expression option the user can enter LIKE and NOT LIKE input parameters both within the single input field.

Please let me know if you any doubt !
Chris Saxon
June 05, 2018 - 1:05 pm UTC

So you are expecting users to enter regular expressions?!

Ouch.

Best send them on regex training. Or - better - change the interface to something more usable...

More to Explore

SQL

The Oracle documentation contains a complete SQL reference.