# Regex blindness

**URL:** <https://www.ruby-forum.com/t/regex-blindness/73949>\
**Category:** Ruby\
**Created:** [October 28, 2006, 9:07am UTC](https://www.ruby-forum.com/t/regex-blindness/73949 "2006-10-28T09:07:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [October 28, 2006, 9:07am UTC](https://www.ruby-forum.com/t/regex-blindness/73949/1 "2006-10-28T09:07:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [October 28, 2006, 10:07am UTC](https://www.ruby-forum.com/t/regex-blindness/73949/2 "2006-10-28T10:07:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [October 29, 2006, 7:30am UTC](https://www.ruby-forum.com/t/regex-blindness/73949/3 "2006-10-29T07:30:32Z")

</div>

On 2006-10-28 10:11:49 +0200, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
said:

> Ruby does not allow the “k” flag there. And I believe Ruby does not  
> 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

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [October 28, 2006, 10:17am UTC](https://www.ruby-forum.com/t/regex-blindness/73949/4 "2006-10-28T10:17:08Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [October 29, 2006, 7:09pm UTC](https://www.ruby-forum.com/t/regex-blindness/73949/5 "2006-10-29T19:09:30Z")

</div>

Josselin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 /@/.
