Re: OT - Regular Expression

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

I was not having PHP on the machine I am reading this email for the moment. But I tried to use it together with egrep, but it didn't work. Maybe egrep is using different syntax for these lookaheads, but with other tests it has been working same as for preg_match in PHP. As you see the first simple example works, but fails when I insert the lookahead.

[peternokia@localhost ~]$ echo 0123 | egrep '^[0-9]{4}$'
0123
[peternokia@localhost ~]$ echo 0123 | egrep '^(?=.*2.*)[0-9]{4}$'
[peternokia@localhost ~]$

/Peter



Quoting Martin Alterisio <malterisio777@xxxxxxxxx>:

PS: I think you can remove the last .*, leaving the assertion like this:
(?=.*8), and it will still work fine and probably faster (which dosen't
matter under these conditions). But I haven't tried that one (and have
already erased the test file I did to check the regular expression).

2007/2/9, Martin Alterisio <malterisio777@xxxxxxxxx>:

If you want to do it in one regular expression, without listing each case,
you can use a lookahead assertion:

/^(?=.*8.*)[0-9]{4}$/

The assertion (?=.*8.*) checks that the following matches the expression
contained (.*8.*) which fails if there is not an 8.

2007/2/9, Peter Lauri <lists@xxxxxxxxxxx>:

Best group member,



I want to match a four digit number. I allow user to enter with *
syntax. So
8* would match anything that starts with 8 and is 4 digit long so:



/^8[0-9]{3}$/



That was easy. Ok then my other case was: *8, so anything that ends with
8



/^[0-9]{3}8$/



Ok, now the tricky one comes: *8*, so match it incase 8 is anywhere in
the
number. Can be beginning, end or in the middle. The problem that I face
I
cannot find out a good way of doing this correctly. So I ended up with
an
expression like this:



/^(8[0-9]{3}|[0-9]8[0-9]{2}|[0-9]{2}8[0-9]|[0-9]{3}8)$/



This takes care of it and everything, BUT it is so ugly. What I actually

need to construct is: A regular expression that checks if 8 is a part of
the
number, and then that it is four digit long.



The pipe "|" is an OR operator, but are there not any "AND" operator in
Regular Expressions? I have been trying to figure this out for a while
now.
Of course I am using the above syntax right now, but would like to strip
it
down. Maybe not for the performance, but for the beauty of it :-)



If you have any comments and suggestions about this I would be happy.



Best regards,

Peter Lauri



<http://www.dwsasia.com/> www.dwsasia.com - company web site

<http://www.lauri.se/> www.lauri.se - personal web site

< http://www.carbonfree.org.uk/> www.carbonfree.org.uk - become Carbon
Free






--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux