tags:

views:

47

answers:

2

I have a table with the following values:

ID     NAME        ADDRESS
1      Bob         Super stree1 here goes
2      Alice       stree100 here goes
3      Clark       Fast left stree1005
4      Magie       Right stree1580 here goes

I need to make a query using LIKE and get only the row having stree1 (in this case only get the one with ID=1) And I use the following query;

select * from table t1 WHERE t1.ADDRESS  LIKE '%stree1%';

But the problem is that I get all rows as each of them contains stree1 plus some char/number after.

I have found out that I can use REGEXP_LIKE as I am using oracle, what would be the proper regex to use in:

select * from table t1 WHERE regexp_like(t1.ADDRESS ,'stree1');

Thanx for your time.

+4  A: 

I would think that this would be the reg-ex you are seeking:

select * from table t1 WHERE regexp_like(t1.ADDRESS ,'stree1(?:[^[:word:]]|$)');

If you want to, you can further simplify this to:

select * from table t1 WHERE regexp_like(t1.ADDRESS ,'stree1(?:\W|$)');

That is, 'stree1' is not followed by a word character (i.e., is followed by space/punctuation/etc...) or 'stree1' appears at the end of the string. Of course there are many other ways to do the same thing, including word boundaries 'stree1\b', expecting particular characters after the 1 in stree1 (e.g., a white-space with 'stree1\s'), etc...

Michael Goldshteyn
thanx @Michael that is exactly what I need.
Adnan
That may return the character after stree1 (non-word or EOL). You may need to use stree1(?=\W|$) if it does add unwanted characters or use stree1\b which matches any word boundary.
tinifni
In this SQL query, it's not a matter of what the regex would "return", but simply what it would match. If he was actually capturing, then a forward lookahead would be more appropriate.
Michael Goldshteyn
Oh, that makes sense! Thanks for explaining that to me.
tinifni
A: 

This may help:

stree1\b
tinifni