For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > January 2007 > ereg question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author ereg question
monomaniac21

2007-01-26, 6:59 pm

hi all

can anyone tell me, does this condition evaluate to true or false?

$foo = ereg('ca?t', 'caaaaaaat');

thanks

marc

Rik

2007-01-26, 6:59 pm

monomaniac21 <mcyi2mr3@googlemail.com> wrote:

> hi all
>
> can anyone tell me, does this condition evaluate to true or false?
>
> $foo =3D ereg('ca?t', 'caaaaaaat');


False afaik. Forget the POSIX functions, use preg_match();

To match 'cat' or 'ct':
preg_match('/ca?t/',$string);

To match 'cat', 'caat','caaat' etc.
preg_match('/ca+t/',$string);

To match 'ct','cat', 'caat','caaat' etc.
preg_match('/ca*t/',$string);
-- =

Rik Wasmus
mvandenb@gmail.com

2007-01-26, 6:59 pm

Regex reference:
http://en.wikipedia.org/wiki/Regular_expression
It basic but it works.


'ca?t' translated in to English:

find the string 'ct' or 'cat'

? - match 1 or 0 occurrences

+ - match 1 or more occurrences 'cat' 'ca...at'

* - match 0 or more occurrences 'ct' 'cat' 'ca...at'

There is more but see the link above.

kenleycapps@gmail.com

2007-01-27, 9:59 pm

If you're looking to interactively test regex, why not use something
like Regex Coach? http://weitz.de/regex-coach/

Very good tool. I can't live without it. Windows only though, as far
as I know.

On Jan 26, 2:01 pm, mvand...@gmail.com wrote:
> Regex reference:http://en.wikipedia.org/wiki/Regular_expression
> It basic but it works.
>
> 'ca?t' translated in to English:
>
> find the string 'ct' or 'cat'
>
> ? - match 1 or 0 occurrences
>
> + - match 1 or more occurrences 'cat' 'ca...at'
>
> * - match 0 or more occurrences 'ct' 'cat' 'ca...at'
>
> There is more but see the link above.


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com