# Regular expression: Is this a bug or feature?

**URL:** <https://rubytalk.org/t/regular-expression-is-this-a-bug-or-feature/34722>\
**Category:** ruby-talk\
**Created:** [23 January 2007 08:20 UTC](https://rubytalk.org/t/regular-expression-is-this-a-bug-or-feature/34722 "2007-01-23T08:20:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_Kahlert1](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@Martin\_Kahlert1](https://rubytalk.org/u/Martin_Kahlert1)\
**Post date:** [23 January 2007 08:20 UTC](https://rubytalk.org/t/regular-expression-is-this-a-bug-or-feature/34722/1 "2007-01-23T08:20:16Z")

</div>

Hi ruby experts!

Is this intended behaviour?

irb(main):001:0\> s1='a=1'  
=\> "a=1"  
irb(main):002:0\> s2='b=1'  
=\> "b=1"  
irb(main):003:0\> s1 =~ /a|b=(.)/  
=\> 0 \<------ expression matches  
irb(main):004:0\> $1  
=\> nil \<------ but where is argument?  
irb(main):005:0\> s2 =~ /a|b=(.)/  
=\> 0 \<------ expression matches  
irb(main):006:0\> $1  
=\> "1" \<------ this has been expected  
irb(main):007:0\> s1 =~ /(a|b)=(.)/  
=\> 0 \<------ expression matches  
irb(main):012:0\> $2  
=\> "1" \<------ this has been expected

Tested on ruby 1.8.2 (2004-12-22) [i686-linux]

Thanks for your help in advance  
Martin.

---

<div class="post-metadata">

**Author:** ![Sylvain\_Joyeux](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@Sylvain\_Joyeux](https://rubytalk.org/u/Sylvain_Joyeux)\
**Post date:** [23 January 2007 08:40 UTC](https://rubytalk.org/t/regular-expression-is-this-a-bug-or-feature/34722/2 "2007-01-23T08:40:47Z")

</div>

> irb(main):001:0\> s1='a=1'  
> =\> "a=1"  
> irb(main):003:0\> s1 =~ /a|b=(.)/  
> =\> 0 \<------ expression matches  
> irb(main):004:0\> $1  
> =\> nil \<------ but where is argument?

You wanted to do s1 =~ /(?:a|b)=(.)/. The | operator has a low precedence  
in regexps

> **···**
>
> --  
> Sylvain Joyeux
