[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

regex blindness

Josselin

10/28/2006 7:04:00 AM

I was blind yesterday .. could not find what's wrong in this regex (and
posted that to mac os forum)

/^(?k: ( ?k:0[1-9]|[1-8][0-9]|9[0-8] ) (?k: [0-9]{3} ) )$/

trying to check zipcodes (french cities : like 78231)

got an undefined (?...) sequence:
/^(?k:(?k:0[1-9]|[1-8][0-9]|9[0-8])(?k:[0-9]{3}))$/

I am also checking email addresses with :
/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\.\-])+\.)+([a-zA-Z0-9]{2,4})+$/

is there a better regexp for that ...

thanks
joss

4 Answers

Paul Lutus

10/28/2006 8:02:00 AM

0

Josselin wrote:


> is there a better regexp for that ...

State the problem, not the solution. Describe the exact requirements to be
met. Give an example of the data to be tested. Then someone will offer a
working regular expression.

--
Paul Lutus
http://www.ara...

Robert Klemme

10/28/2006 8:12:00 AM

0

Josselin wrote:
> I was blind yesterday .. could not find what's wrong in this regex (and
> posted that to mac os forum)
>
> /^(?k: ( ?k:0[1-9]|[1-8][0-9]|9[0-8] ) (?k: [0-9]{3} ) )$/
>
> trying to check zipcodes (french cities : like 78231)
>
> got an undefined (?...) sequence:
> /^(?k:(?k:0[1-9]|[1-8][0-9]|9[0-8])(?k:[0-9]{3}))$/

irb(main):001:0> /(?:x)/
=> /(?:x)/
irb(main):002:0> /(?k:x)/
SyntaxError: compile error
(irb):2: undefined (?...) sequence: /(?k:x)/
from (irb):2
from :0
irb(main):003:0> /(?i:x)/
=> /(?i:x)/

Ruby does not allow the "k" flag there. And I believe Ruby does not
have that RX flag at all.

> I am also checking email addresses with :
> /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\.\-])+\.)+([a-zA-Z0-9]{2,4})+$/
>
> is there a better regexp for that ...

I guess you'll find out plenty out there. The answer to "better"
heavily depends on what your goals are (simpler RX, faster matching),
what RX engine you are using and what data you are processing. Example:
in some cases /[^@\s]+@[^@\s]+/ might be perfectly ok to match an email
address - in other cases it won't.

Kind regards

robert

Josselin

10/29/2006 6:29:00 AM

0

On 2006-10-28 10:11:49 +0200, Robert Klemme <shortcutter@googlemail.com> said:

> Josselin wrote:
>> I was blind yesterday .. could not find what's wrong in this regex (and
>> posted that to mac os forum)
>>
>> /^(?k: ( ?k:0[1-9]|[1-8][0-9]|9[0-8] ) (?k: [0-9]{3} ) )$/
>>
>> trying to check zipcodes (french cities : like 78231)
>>
>> got an undefined (?...) sequence:
>> /^(?k:(?k:0[1-9]|[1-8][0-9]|9[0-8])(?k:[0-9]{3}))$/
>
> irb(main):001:0> /(?:x)/
> => /(?:x)/
> irb(main):002:0> /(?k:x)/
> SyntaxError: compile error
> (irb):2: undefined (?...) sequence: /(?k:x)/
> from (irb):2
> from :0
> irb(main):003:0> /(?i:x)/
> => /(?i:x)/
>
> Ruby does not allow the "k" flag there. And I believe Ruby does not
> have that RX flag at all.
>
>> I am also checking email addresses with :
>> /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\.\-])+\.)+([a-zA-Z0-9]{2,4})+$/
>>
>> is there a better regexp for that ...
>
> I guess you'll find out plenty out there. The answer to "better"
> heavily depends on what your goals are (simpler RX, faster matching),
> what RX engine you are using and what data you are processing.
> Example: in some cases /[^@\s]+@[^@\s]+/ might be perfectly ok to match
> an email address - in other cases it won't.
>
> Kind regards
>
> robert

thanks for your advice, removing the k flag was the k-point..
joss

Christian Neukirchen

10/29/2006 6:07:00 PM

0

Josselin <josselin@wanadoo.fr> writes:

> I am also checking email addresses with :
> /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\.\-])+\.)+([a-zA-Z0-9]{2,4})+$/
>
> is there a better regexp for that ...

Yes, try /@/.

> thanks
> joss
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...