Regular Expression for 'and' instead of 'or'

Поиск
Список
Период
Сортировка
Искать

Regular Expression for 'and' instead of 'or'

От:
"Samuel J. Sutjiono" <ssutjiono@wc-group.com>
Дата:
This expression matches the word socks or shoes or nike in product category 
where productdescr ~* '(socks|shoes|nike)'
 
Does anybody know what the expression should be  if I want to do 'and' of those key words instead of 'or' ?
 
Thanks,
Sam 

Re: [GENERAL] Regular Expression for 'and' instead of 'or'

От:
Fernando Schapachnik <fschapachnik@vianetworks.com.ar>
Дата:
En un mensaje anterior, Samuel J. Sutjiono escribió:
> This expression matches the word socks or shoes or nike in product category 
> where productdescr ~* '(socks|shoes|nike)'
> 
> Does anybody know what the expression should be  if I want to do 'and' of those key words instead of 'or' ?

You have to use something like:

'(socks.*shoes.*nike)|(socks.*nike.*shoes)|...'

where ... is every other possible combination. Not pretty, but that's
a limitation of finite automata (or regular expressions, which are
the same).

Regards.



Fernando P. Schapachnik
Gerente de tecnología de red
y sistemas de información
VIA NET.WORKS ARGENTINA S.A.
fschapachnik@vianetworks.com.ar
Tel.: (54-11) 4323-3381

Re: [GENERAL] Regular Expression for 'and' instead of 'or'

От:
"Samuel J. Sutjiono" <ssutjiono@wc-group.com>
Дата:
Thank you very much Fernando.  Do you know whether this expression (regex)
use index scan ?

Regards,
Sam
----- Original Message -----
From: "Fernando Schapachnik" 
To: "Samuel J. Sutjiono" 
Cc: ; 
Sent: Friday, February 22, 2002 1:15 PM
Subject: Re: [SQL] [GENERAL] Regular Expression for 'and' instead of 'or'


> En un mensaje anterior, Samuel J. Sutjiono escribió:
> > This expression matches the word socks or shoes or nike in product
category
> > where productdescr ~* '(socks|shoes|nike)'
> >
> > Does anybody know what the expression should be  if I want to do 'and'
of those key words instead of 'or' ?
>
> You have to use something like:
>
> '(socks.*shoes.*nike)|(socks.*nike.*shoes)|...'
>
> where ... is every other possible combination. Not pretty, but that's
> a limitation of finite automata (or regular expressions, which are
> the same).
>
> Regards.
>
>
>
> Fernando P. Schapachnik
> Gerente de tecnología de red
> y sistemas de información
> VIA NET.WORKS ARGENTINA S.A.
> fschapachnik@vianetworks.com.ar
> Tel.: (54-11) 4323-3381
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
>     (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)
>

Re: [GENERAL] Regular Expression for 'and' instead of 'or'

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Fernando Schapachnik  writes:
> You have to use something like:

> '(socks.*shoes.*nike)|(socks.*nike.*shoes)|...'

> where ... is every other possible combination. Not pretty, but that's
> a limitation of finite automata (or regular expressions, which are
> the same).

Seems a lot easier to do

	WHERE field ~* 'socks' AND field ~* 'shoes' AND field ~* 'nike'

			regards, tom lane

Re: Regular Expression for 'and' instead of 'or'

От:
Peter Eisentraut <peter_e@gmx.net>
Дата:
Samuel J. Sutjiono writes:

> This expression matches the word socks or shoes or nike in product category
> where productdescr ~* '(socks|shoes|nike)'
>
> Does anybody know what the expression should be if I want to do 'and'
> of those key words instead of 'or' ?

You probably want something like

productdescr ~* 'socks' AND productdescr ~* 'shoes' AND productdescr ~* 'nike'

I don't think it's possible to do this for the general case with just a
regular expression.

-- 
Peter Eisentraut   peter_e@gmx.net

Re: Regular Expression for 'and' instead of 'or'

От:
Rajesh Kumar Mallah <mallah@trade-india.com>
Дата:
 How about  using the  contrib/tsearch  module 
that uses gist indexes.

moreover it has  builtin support for word stemming,morphology
and stuff. (to my knowledge)

you would ofcourse require to upgrade to PGSQL7.2
 

excrepts from README.tsearch contrib module.
================
and now you can search all titles with words 'patch' and 'gist':
  select title from titles where titleidx ## 'patch&gist';

Here, ## is a new operation defined for type 'txtidx' which could use index
(if exists) built on titleidx. This operator uses morphology to
expand query, i.e.
  ## 'patches&gist' will find titles with 'patch' and 'gist' also.
If you want to provide query as is, use operator @@ instead:
  select title from titles where titleidx @@ 'patch&gist';
 
 

"Samuel J. Sutjiono" wrote:

This expression matches the word socks or shoes or nike in product categorywhere productdescr ~* '(socks|shoes|nike)' Does anybody know what the expression should be  if I want to do 'and' of those key words instead of 'or' ? Thanks,Sam

Re: [GENERAL] Regular Expression for 'and' instead of 'or'

От:
Fernando Schapachnik <fschapachnik@vianetworks.com.ar>
Дата:
En un mensaje anterior, Samuel J. Sutjiono escribió:
> Thank you very much Fernando.  Do you know whether this expression (regex)
> use index scan ?

Not sure, but probably an EXPLAIN will tell.

Good luck.

Fernando P. Schapachnik
Gerente de tecnología de red
y sistemas de información
VIA NET.WORKS ARGENTINA S.A.
fschapachnik@vianetworks.com.ar
Tel.: (54-11) 4323-3381

Re: [GENERAL] Regular Expression for 'and' instead of 'or'

От:
Medi Montaseri <medi@cybershell.com>
Дата:
Based on generic rules of Regular Expression, the default is a AND.
For example

RE-1RE-2RE-3 is really (RE-1) and (RE-2) and (RE-3). However Regular
Expression
also provides the "Alternation Metachar" to change it from a default AND
to a OR.

The problem however is if you have target-1 junk target-2 junk junk
target-3
Then the simple (target-1target-2target-3) would not work, as you are
implying
they are following each other. So the fix could be

productdescr ~* '(socks)' AND productdescr ~* '(shoes)' AND productdescr
~*(nike)'

Effectively you are creating a composite expression consisting of three
sub-expression.

"Samuel J. Sutjiono" wrote:

> This expression matches the word socks or shoes or nike in product
> categorywhere productdescr ~* '(socks|shoes|nike)' Does anybody know
> what the expression should be  if I want to do 'and' of those key
> words instead of 'or' ? Thanks,Sam

--
-------------------------------------------------------------------------
Medi Montaseri                               medi@CyberShell.com
Unix Distributed Systems Engineer            HTTP://www.CyberShell.com
CyberShell Engineering
-------------------------------------------------------------------------



FAQ