views:

102

answers:

2

I'm looking for an Oracle Regular Expression that will match tokens in any order.

For example, say I'm looking for "one two".

I would want it to match both, "one token two" "two other one"

The number of tokens might grow larger than two, so generating the permutations for the regex would be a hassel.

Is there an easier way to do this, than this

'(ONE.*TWO)|(TWO.*ONE)'

 i.e

select * 
from some_table t
where regexp_like(t.NAME_KEY, '(ONE.*TWO)|(TWO.*ONE)')
+1  A: 

You can use several different regular expressions:

SELECT * 
FROM some_table t
WHERE regexp_like(t.NAME_KEY, 'ONE')
AND regexp_like(t.NAME_KEY, 'TWO')

One issue is that this will also match 'TWONE' which the original regular expression would not match. This can be fixed if you also check for some separating tokens or word boundary.

Also a regular expression is not necessary to match a constant string. You could just use LIKE instead.

Mark Byers
Just to add to this you can fix the 'TWONE' issue like this: `regexp_like(t.NAME_KEY, '(^|\s)ONE($|\s)')` a bit crude but it should work as desired. Doing lots of regexp_like would probably be a big performance hit. How many do you plan on doing? It might be better exploring other methods, storing the data in a more manageable form.
Gary Green
I will be performing this operation on a very large table. Performance is a definate concern. I was thinking the regex would perform better than several LIKE's.
Andy Pryor
@Andy Pryor: I can almost guarantee that [Oracle's Full Text Search (FTS)](http://www.oracle-base.com/articles/9i/FullTextIndexingUsingOracleText9i.php) will out-perform equivalent REGEX or LIKE functionality.
OMG Ponies
@OMG Ponies: I'd love to see an answer using FTS. One of these days I need to explore that technology.
Shannon Severance
+5  A: 

Here's an alternative query that uses Full Text Search (FTS) functionality:

WHERE CONTAINS(t.name_key, 'ONE & TWO') > 0

See the Precedence Examples for criteria evaluation explanation.

Related:

OMG Ponies
@Shannon Severance: Ask and ye shall receive ^
OMG Ponies