# regex

**URL:** <https://rubytalk.org/t/regex/75270>\
**Category:** ruby-talk\
**Created:** [16 June 2020 10:44 UTC](https://rubytalk.org/t/regex/75270 "2020-06-16T10:44:43Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Die\_Optimisten](https://avatars.discourse-cdn.com/v4/letter/d/f04885/32.png) [@Die\_Optimisten](https://rubytalk.org/u/Die_Optimisten)\
**Post date:** [16 June 2020 10:44 UTC](https://rubytalk.org/t/regex/75270/1 "2020-06-16T10:44:43Z")

</div>

Hello,

str=~/str1/ && str=~/str2/ && str=~/str3/ ... && str=~/strN/ ?  
Is there a method for that like str.match\_all(re1,re2,...);  
str.include\_all(str1,str2,...) or a shorter form?

Another solution using metaprogramming?

thanks  
Andrew

---

<div class="post-metadata">

**Author:** ![Tom\_Lord](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/tom_lord/32/2215_2.png) [@Tom\_Lord](https://rubytalk.org/u/Tom_Lord)\
**Post date:** [16 June 2020 10:48 UTC](https://rubytalk.org/t/regex/75270/2 "2020-06-16T10:48:08Z")

</div>

I would use Enumerable#all? For this:

[/str1/, /str2, /str3/, …, /strN/].all? { |pattern| str.match?(pattern) }

Regards,  
Tom.

> **···**
>
> > On 16 Jun 2020, at 11:44, Die Optimisten \<inform@die-optimisten.net\> wrote:
> > 
> > Hello,
> > 
> > str=~/str1/ && str=~/str2/ && str=~/str3/ ... && str=~/strN/ ?  
> > Is there a method for that like str.match\_all(re1,re2,...);  
> > str.include\_all(str1,str2,...) or a shorter form?
> > 
> > Another solution using metaprogramming?
> > 
> > thanks  
> > Andrew
> > 
> > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>  
> > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);

---

<div class="post-metadata">

**Author:** ![Dave\_Aronson](https://avatars.discourse-cdn.com/v4/letter/d/7ba0ec/32.png) [@Dave\_Aronson](https://rubytalk.org/u/Dave_Aronson)\
**Post date:** [16 June 2020 12:18 UTC](https://rubytalk.org/t/regex/75270/3 "2020-06-16T12:18:13Z")

</div>

You're already using regexes. You want \*three\* problems? 😉

> **···**
>
> On Tue, Jun 16, 2020 at 6:45 AM Die Optimisten \<inform@die-optimisten.net\> wrote:
> 
> > Another solution using metaprogramming?
> 
> --  
> Dave Aronson, T. Rex  
> Codosaurus, LLC ([http://www.Codosaur.us](http://www.Codosaur.us))  
> Software Development Consulting  
> How can we evolve you today?

---

<div class="post-metadata">

**Author:** ![Die\_Optimisten](https://avatars.discourse-cdn.com/v4/letter/d/f04885/32.png) [@Die\_Optimisten](https://rubytalk.org/u/Die_Optimisten)\
**Post date:** [16 June 2020 15:04 UTC](https://rubytalk.org/t/regex/75270/4 "2020-06-16T15:04:59Z")

</div>

Why three problems ? 🙂

1) Regex per se?

2)

3)

> **···**
>
> Am 6/16/20 um 2:18 PM schrieb Dave Aronson:
> 
> > On Tue, Jun 16, 2020 at 6:45 AM Die Optimisten \> \<inform@die-optimisten.net\> wrote:
> > 
> > > Another solution using metaprogramming?
> > 
> > You're already using regexes. You want \*three\* problems? 😉

---

<div class="post-metadata">

**Author:** ![Walter\_Lee\_Davis](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/walter_lee_davis/32/1974_2.png) [@Walter\_Lee\_Davis](https://rubytalk.org/u/Walter_Lee_Davis)\
**Post date:** [16 June 2020 15:15 UTC](https://rubytalk.org/t/regex/75270/5 "2020-06-16T15:15:51Z")

</div>

> **[Perl Problems](https://xkcd.com/1171/)**
>
> To generate #1 albums, 'jay --help' recommends the -z flag.

> **···**
>
> > On Jun 16, 2020, at 11:04 AM, Die Optimisten \<inform@die-optimisten.net\> wrote:
> > 
> > Am 6/16/20 um 2:18 PM schrieb Dave Aronson:
> > 
> > > On Tue, Jun 16, 2020 at 6:45 AM Die Optimisten \>\> \<inform@die-optimisten.net\> wrote:
> > > 
> > > > Another solution using metaprogramming?
> > > 
> > > You're already using regexes. You want \*three\* problems? 😉
> > 
> > Why three problems ? 🙂
> > 
> > 1) Regex per se?
> > 
> > 2)
> > 
> > 3)
> > 
> > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>  
> > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);

---

<div class="post-metadata">

**Author:** ![Jack\_Royal-Gordon](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/jack_royal-gordon/32/2187_2.png) [@Jack\_Royal-Gordon](https://rubytalk.org/u/Jack_Royal-Gordon)\
**Post date:** [16 June 2020 22:50 UTC](https://rubytalk.org/t/regex/75270/6 "2020-06-16T22:50:48Z")

</div>

Not exactly what you’re asking for, but worth noting: "str =~ RegExp.union(/re1/, /re2/, …)" is equivalent to "str =~ /re1/ || str =~ /re2/ …”, that is, the same as you require except for logical or-ing of result instead of and-ing. My understanding that this is much faster than either coding by hand or using ".all?”.

> **···**
>
> > On Jun 16, 2020, at 8:15 AM, Walter Lee Davis \<waltd@wdstudio.com\> wrote:
> > 
> > [xkcd: Perl Problems](https://xkcd.com/1171/)
> > 
> > > On Jun 16, 2020, at 11:04 AM, Die Optimisten \<inform@die-optimisten.net\> wrote:
> > > 
> > > Am 6/16/20 um 2:18 PM schrieb Dave Aronson:
> > > 
> > > > On Tue, Jun 16, 2020 at 6:45 AM Die Optimisten \>\>\> \<inform@die-optimisten.net\> wrote:
> > > > 
> > > > > Another solution using metaprogramming?
> > > > 
> > > > You're already using regexes. You want \*three\* problems? 😉
> > > 
> > > Why three problems ? 🙂
> > > 
> > > 1) Regex per se?
> > > 
> > > 2)
> > > 
> > > 3)
> > > 
> > > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>  
> > > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);
> > 
> > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>  
> > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);

---

<div class="post-metadata">

**Author:** ![Sean\_Felipe\_Wolfe](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/sean_felipe_wolfe/32/1952_2.png) [@Sean\_Felipe\_Wolfe](https://rubytalk.org/u/Sean_Felipe_Wolfe)\
**Post date:** [16 June 2020 23:48 UTC](https://rubytalk.org/t/regex/75270/7 "2020-06-16T23:48:32Z")

</div>

love it 🙂

> **···**
>
> On Tue, Jun 16, 2020 at 8:16 AM Walter Lee Davis \<waltd@wdstudio.com\> wrote:
> 
> > [xkcd: Perl Problems](https://xkcd.com/1171/)
> > 
> > \> On Jun 16, 2020, at 11:04 AM, Die Optimisten \<inform@die-optimisten.net\> \> wrote:  
> > \>  
> > \> Am 6/16/20 um 2:18 PM schrieb Dave Aronson:  
> > \>\> On Tue, Jun 16, 2020 at 6:45 AM Die Optimisten \> \>\> \<inform@die-optimisten.net\> wrote:  
> > \>\>  
> > \>\>\> Another solution using metaprogramming?  
> > \>\> You're already using regexes. You want \*three\* problems? 😉  
> > \>  
> > \> Why three problems ? 🙂  
> > \>  
> > \> 1) Regex per se?  
> > \>  
> > \> 2)  
> > \>  
> > \> 3)  
> > \>  
> > \>  
> > \>  
> > \> Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org  
> > ?subject=unsubscribe\>  
> > \> \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);
> > 
> > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>  
> > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);
> 
> --  
> A musician must make music, an artist must paint, a poet must write, if he  
> is to be ultimately at peace with himself.  
> - Abraham Maslow

---

<div class="post-metadata">

**Author:** ![Die\_Optimisten](https://avatars.discourse-cdn.com/v4/letter/d/f04885/32.png) [@Die\_Optimisten](https://rubytalk.org/u/Die_Optimisten)\
**Post date:** [17 June 2020 07:52 UTC](https://rubytalk.org/t/regex/75270/8 "2020-06-17T07:52:12Z")

</div>

> ... that is, the same as you require except for logical or-ing of result instead of and-ing. My understanding that this is much faster than either coding by hand or using ".all?”.

So shouldn't there be an equivalent (fast) method for '&&' ?

'||' should never have precendence over '&&' ;))

Andrew

> **···**
>
> Am 6/17/20 um 12:50 AM schrieb Jack Royal-Gordon:
> 
> > > On Jun 16, 2020, at 8:15 AM, Walter Lee Davis \<waltd@wdstudio.com\> wrote:
> > > 
> > > [xkcd: Perl Problems](https://xkcd.com/1171/)
> > > 
> > > > On Jun 16, 2020, at 11:04 AM, Die Optimisten \<inform@die-optimisten.net\> wrote:
> > > > 
> > > > Am 6/16/20 um 2:18 PM schrieb Dave Aronson:
> > > > 
> > > > > On Tue, Jun 16, 2020 at 6:45 AM Die Optimisten \>\>\>\> \<inform@die-optimisten.net\> wrote:
> > > > > 
> > > > > > Another solution using metaprogramming?
> > > > > 
> > > > > You're already using regexes. You want \*three\* problems? 😉
> > > > 
> > > > Why three problems ? 🙂
> > > > 
> > > > 1) Regex per se?
> > > > 
> > > > 2)
> > > > 
> > > > 3)

---

<div class="post-metadata">

**Author:** ![gvim](https://avatars.discourse-cdn.com/v4/letter/g/a3d4f5/32.png) [@gvim](https://rubytalk.org/u/gvim)\
**Post date:** [17 June 2020 15:44 UTC](https://rubytalk.org/t/regex/75270/9 "2020-06-17T15:44:57Z")

</div>

If you're only matching with strings you can dispense with Regexp altogether:

['str1', 'str2', 'str3'].each {|s| 'xxxstr1xxxstr2xxxstr3'.include? s}

gvim

> **···**
>
> On 16/06/2020 11:44, Die Optimisten wrote:
> 
> > Hello,
> > 
> > str=~/str1/ && str=~/str2/ && str=~/str3/ ... && str=~/strN/ ?  
> > Is there a method for that like str.match\_all(re1,re2,...);  
> > str.include\_all(str1,str2,...) or a shorter form?
> > 
> > Another solution using metaprogramming?
> > 
> > thanks  
> > Andrew

---

<div class="post-metadata">

**Author:** ![Jack\_Royal-Gordon](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/jack_royal-gordon/32/2187_2.png) [@Jack\_Royal-Gordon](https://rubytalk.org/u/Jack_Royal-Gordon)\
**Post date:** [17 June 2020 17:59 UTC](https://rubytalk.org/t/regex/75270/10 "2020-06-17T17:59:08Z")

</div>

If you’re designing the language from a “completeness” approach, rather than addressing a particular need, you would be correct. But it seems to me that there are many more usage cases for ORing of regex tests rather than for ANDing them.

> **···**
>
> > On Jun 17, 2020, at 12:52 AM, Die Optimisten \<inform@die-optimisten.net\> wrote:
> > 
> > Am 6/17/20 um 12:50 AM schrieb Jack Royal-Gordon:
> > 
> > > ... that is, the same as you require except for logical or-ing of result instead of and-ing. My understanding that this is much faster than either coding by hand or using ".all?”.
> > 
> > So shouldn't there be an equivalent (fast) method for '&&' ?
> > 
> > '||' should never have precendence over '&&' ;))
> > 
> > Andrew
> > 
> > > > On Jun 16, 2020, at 8:15 AM, Walter Lee Davis \<waltd@wdstudio.com\> wrote:
> > > > 
> > > > [xkcd: Perl Problems](https://xkcd.com/1171/)
> > > > 
> > > > > On Jun 16, 2020, at 11:04 AM, Die Optimisten \<inform@die-optimisten.net\> wrote:
> > > > > 
> > > > > Am 6/16/20 um 2:18 PM schrieb Dave Aronson:
> > > > > 
> > > > > > On Tue, Jun 16, 2020 at 6:45 AM Die Optimisten \>\>\>\>\> \<inform@die-optimisten.net\> wrote:
> > > > > > 
> > > > > > > Another solution using metaprogramming?
> > > > > > 
> > > > > > You're already using regexes. You want \*three\* problems? 😉
> > > > > 
> > > > > Why three problems ? 🙂
> > > > > 
> > > > > 1) Regex per se?
> > > > > 
> > > > > 2)
> > > > > 
> > > > > 3)
> > 
> > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>  
> > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://rubytalk.org/u/Martin_DeMello)\
**Post date:** [17 June 2020 18:39 UTC](https://rubytalk.org/t/regex/75270/11 "2020-06-17T18:39:08Z")

</div>

"Some people, when confronted with a problem, think “I know, I'll use  
regular expressions.” Now they have two problems."

[Jeffrey Friedl's Blog » Source of the famous “Now you have two problems” quote](http://regex.info/blog/2006-09-15/247) has a good write up on the history of  
the quote.

martin

> **···**
>
> On Tue, Jun 16, 2020 at 8:05 AM Die Optimisten \<inform@die-optimisten.net\> wrote:
> 
> > Am 6/16/20 um 2:18 PM schrieb Dave Aronson:  
> > \> On Tue, Jun 16, 2020 at 6:45 AM Die Optimisten \> \> \<inform@die-optimisten.net\> wrote:  
> > \>  
> > \>\> Another solution using metaprogramming?  
> > \> You're already using regexes. You want \*three\* problems? 😉
> > 
> > Why three problems ? 🙂
> > 
> > 1) Regex per se?
> > 
> > 2)
> > 
> > 3)
> > 
> > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>  
> > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);

---

<div class="post-metadata">

**Author:** ![Sebastian\_Hungereck1](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastian\_Hungereck1](https://rubytalk.org/u/Sebastian_Hungereck1)\
**Post date:** [18 June 2020 09:50 UTC](https://rubytalk.org/t/regex/75270/12 "2020-06-18T09:50:53Z")

</div>

> > ... that is, the same as you require except for logical or-ing of  
> > result instead of and-ing. My understanding that [Regexp.union] is

much faster

> > than either coding by hand or using ".all?”.
> 
> So shouldn't there be an equivalent (fast) method for '&&' ?

What Regexp.union does is basically to create a new regex by joining the  
given regexen with "|" (in a flag-aware way). So Regexp.union(/a/, /b/)  
would return a regex equivalent to /a|b/. (Note that Regexp.union is  
only actually useful when at least some of the arguments are variables -  
in the given example you could just write `/a|b/` without any need for  
Regexp.union).

Now the reason that there is no equivalent Regexp.intersection is  
that regular expressions have no equivalent & operator, so you can't  
just join the regexen like Regexp.union does. And the reason that  
there is no & operator is that regex intersection is a fundamentally  
more complicated and expensive operation than union. So implementing  
& would take a lot of effort and using it could both lead to large  
compilation times for your regular expressions as well as the resulting  
compiled regex being slow. Therefore implementing intersection for  
regular expressions is generally seen as a bad idea for general purpose  
regex engines.

> **···**
>
> On 17.06.20 09:52, Die Optimisten wrote:
> 
> > Am 6/17/20 um 12:50 AM schrieb Jack Royal-Gordon:

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [20 June 2020 23:41 UTC](https://rubytalk.org/t/regex/75270/13 "2020-06-20T23:41:01Z")

</div>

ANDing is often not possible or not useful. Consider matching /^foo/ and  
/^bar/ - this will never yield true. If you have /^foo/ and /bar$/ then you  
can easily combine into /^foo.\*bar$/ and for /foo/ and /bar/ you need  
/foo.\*bar|bar.\*foo/ - even worse if there are overlaps in the patterns. For  
other combinations of patterns other combination mechanisms might work. The  
situation for AND is very different than that for OR.

Cheers

robert

> **···**
>
> On Wed, Jun 17, 2020 at 7:59 PM Jack Royal-Gordon \<jackrg@pobox.com\> wrote:
> 
> > If you’re designing the language from a “completeness” approach, rather  
> > than addressing a particular need, you would be correct. But it seems to me  
> > that there are many more usage cases for ORing of regex tests rather than  
> > for ANDing them.
> 
> --  
> [guy, jim, charlie, sho].each {|him| remember.him do |as, often| as.you\_can  
> - without end}  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)
