# Re: regex capture and assign... in one line?

**URL:** <https://www.ruby-forum.com/t/re-regex-capture-and-assign-in-one-line/74619>\
**Category:** Ruby\
**Created:** [November 7, 2006, 10:50am UTC](https://www.ruby-forum.com/t/re-regex-capture-and-assign-in-one-line/74619 "2006-11-07T10:50:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [November 7, 2006, 10:50am UTC](https://www.ruby-forum.com/t/re-regex-capture-and-assign-in-one-line/74619/1 "2006-11-07T10:50:19Z")

</div>

From: Nate M. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

> $str = “foo123”;
> 
> my ($foo,$bar) = $str =~ /(\w+)(\d+)/;

irb(main):001:0\> str = “foo123”  
=\> “foo123”  
irb(main):004:0\> \_,foo,bar = /(\w+)(\d+)/.match( str ).to\_a  
=\> [“foo123”, “foo12”, “3”]

Perhaps you meant ([a-z]+), but that’s obviously not the point.

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [November 7, 2006, 10:50am UTC](https://www.ruby-forum.com/t/re-regex-capture-and-assign-in-one-line/74619/2 "2006-11-07T10:50:54Z")

</div>

Gavin K. wrote:

> From: Nate M. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]
> 
> > $str = “foo123”;
> > 
> > my ($foo,$bar) = $str =~ /(\w+)(\d+)/;
> 
> irb(main):001:0\> str = “foo123”  
> =\> “foo123”  
> irb(main):004:0\> \_,foo,bar = /(\w+)(\d+)/.match( str ).to\_a  
> =\> [“foo123”, “foo12”, “3”]

And if you use this a lot, and can include your own mini-library stuff,  
make it simpler:

class Regexp  
def %( str )  
matches = match(str).to\_a  
matches.shift  
matches  
end  
end

foo, bar = /(\D+)(\d+)/ % “foo123”  
p foo, bar  
#=\> “foo”  
#=\> “123”

Or if you like it the other way around:  
class String  
def &( regexp )  
matches = regexp.match( self ).to\_a  
matches.shift  
matches  
end  
end

foo, bar = “foo123” & /(\D+)(\d+)/

I’m not sure if I like ‘%’ better (ala modulo, as in “pare down my  
string and give me whatever the regexp leaves”), or ‘&’ better (ala set  
intersection, as in “find whatever is common between this string and  
regexp”). Up to you to pick from those, or choose another. Non-unary  
operator methods left undefined in core Ruby for String and Regexp:

irb(main):002:0\> NON\_UNARY\_OPERATOR\_METHODS  
=\> [“[]”, “[]=”, “\*\*”, “~”, “\*”, “/”, “%”, “+”, “-”, “\>\>”, “\<\<”, “&”,  
“^”, “|”, “\<=”, “\<”, “\>”, “\>=”, “\<=\>”, “==”, “===”, “=~”]

irb(main):003:0\> NON\_UNARY\_OPERATOR\_METHODS - “”.methods  
=\> [“\*\*”, “~”, “/”, “-”, “\>\>”, “&”, “^”, “|”]

irb(main):004:0\> NON\_UNARY\_OPERATOR\_METHODS - //.methods  
=\> [“[]”, “[]=”, “\*\*”, “\*”, “/”, “%”, “+”, “-”, “\>\>”, “\<\<”, “&”, “^”,  
“|”, “\<=”, “\<”, “\>”, “\>=”, “\<=\>”]

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [November 7, 2006, 10:50am UTC](https://www.ruby-forum.com/t/re-regex-capture-and-assign-in-one-line/74619/3 "2006-11-07T10:50:55Z")

</div>

Phrogz wrote:

> irb(main):002:0\> NON\_UNARY\_OPERATOR\_METHODS  
> =\> ["[]", “[]=”, “\*\*”, “~”, “\*”, “/”, “%”, “+”, “-”, “\>\>”, “\<\<”, “&”,  
> “^”, “|”, “\<=”, “\<”, “\>”, “\>=”, “\<=\>”, “==”, “===”, “=~”]
> 
> irb(main):003:0\> NON\_UNARY\_OPERATOR\_METHODS - “”.methods  
> =\> ["\*\*", “~”, “/”, “-”, “\>\>”, “&”, “^”, “|”]
> 
> irb(main):004:0\> NON\_UNARY\_OPERATOR\_METHODS - //.methods  
> =\> ["[]", “[]=”, “\*\*”, “\*”, “/”, “%”, “+”, “-”, “\>\>”, “\<\<”, “&”, “^”,  
> “|”, “\<=”, “\<”, “\>”, “\>=”, “\<=\>”]

Er, of course ‘~’ shouldn’t be listed in the above, since it is an  
unary operator.
