Contain all of the string listed, regardless of display orde

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
coldwinds
Forum Newbie
Posts: 5
Joined: Sat Jun 21, 2008 2:26 pm

Contain all of the string listed, regardless of display orde

Post by coldwinds »

We know that in [a|b|c] the '|' represent 'or', how to present a 'and' condition to match a string? like a&b&c ? regardless of appearing order of a b c in the string.

How could it been done with regexp?
User avatar
ghurtado
Forum Contributor
Posts: 334
Joined: Wed Jul 23, 2008 12:19 pm

Re: Contain all of the string listed, regardless of display orde

Post by ghurtado »

Regular expressions are always position based. So if you need to account for all combinations of those three letters you need to be explicit.

Code: Select all

[(abc)(acb)(bac)(bca)...]
coldwinds
Forum Newbie
Posts: 5
Joined: Sat Jun 21, 2008 2:26 pm

Re: Contain all of the string listed, regardless of display orde

Post by coldwinds »

Thx for your reply, ghurtado.
User avatar
deejay
Forum Contributor
Posts: 201
Joined: Wed Jan 22, 2003 3:33 am
Location: Cornwall

Re: Contain all of the string listed, regardless of display orde

Post by deejay »

i'm not very good at regex and there's probably loads of ways to skin this cat ( would be interested to read of others to help my learning of regex) but wouldn't something like

Code: Select all

/[A-Ca-c]{3}/

achieve the same and be easier to write
User avatar
ghurtado
Forum Contributor
Posts: 334
Joined: Wed Jul 23, 2008 12:19 pm

Re: Contain all of the string listed, regardless of display orde

Post by ghurtado »

It would not be the same if you need to guarantee that there is at least one of each a, b or c, since "aaa" would match your regexp. At the same time, I don't really know if that was a requirement of the OP, I just kindof assumed.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Contain all of the string listed, regardless of display orde

Post by prometheuzz »

coldwinds wrote:We know that in [a|b|c] the '|' represent 'or', how to present a 'and' condition to match a string? like a&b&c ? regardless of appearing order of a b c in the string.

How could it been done with regexp?
Note that inside the square brackets (character class or character set), the pipe symbol is not a logical OR, but matches just the character '|'.
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Re: Contain all of the string listed, regardless of display orde

Post by GeertDD »

I think this is a job for strpos() rather than a regular expression.
User avatar
deejay
Forum Contributor
Posts: 201
Joined: Wed Jan 22, 2003 3:33 am
Location: Cornwall

Re: Contain all of the string listed, regardless of display orde

Post by deejay »

yeah your right ghurtado.


I get it now :)
Post Reply