# Regular Expressions

**URL:** <https://rubytalk.org/t/regular-expressions/50458>\
**Category:** ruby-talk\
**Created:** [1 December 2008 19:08 UTC](https://rubytalk.org/t/regular-expressions/50458 "2008-12-01T19:08:14Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mmcolli00\_Mom](https://avatars.discourse-cdn.com/v4/letter/m/76d3ee/32.png) [@Mmcolli00\_Mom](https://rubytalk.org/u/Mmcolli00_Mom)\
**Post date:** [1 December 2008 19:08 UTC](https://rubytalk.org/t/regular-expressions/50458/1 "2008-12-01T19:08:14Z")

</div>

Hi everyone.

Just a question about regualar expression in Ruby. Is there a way to  
check in each line of a document for always beginning with "23430000"  
and somewhere on that same line another string 'CodeRed'?

line1: 23430000 @#$#$3455000CodeRed 24AAWERE 740000000

This is what I have so far when I import each textdata from another  
file.

textdata.should =~ /23430000/ |CodeRed/

...the pipe was supposed to determine if CodeRed exists somewhere after  
the identity code 23430000. However, it doesn't work like this. Also for  
my code I am not using OR logic with it.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Yaser\_Sulaiman](https://avatars.discourse-cdn.com/v4/letter/y/7c8e57/32.png) [@Yaser\_Sulaiman](https://rubytalk.org/u/Yaser_Sulaiman)\
**Post date:** [1 December 2008 19:34 UTC](https://rubytalk.org/t/regular-expressions/50458/2 "2008-12-01T19:34:48Z")

</div>

I'm not sure if this is what you are asking for, but /23430000.\*CodeRed/  
will match a line that contains '23430000', followed by zero or more  
characters, and then 'CodeRed'.

try in IRB:

irb(main):001:0\> line1 = '23430000 @#$#$3455000CodeRed 24AAWERE 740000000'  
=\> "23430000 @\#$\#$3455000CodeRed 24AAWERE 740000000"  
irb(main):002:0\> line1 =~ /23430000.\*CodeRed/  
=\> 0  
irb(main):003:0\> line2 = '23430000 @#$#$3455000CodeGreen 24AAWERE 740000000'  
=\> "23430000 @\#$\#$3455000CodeGreen 24AAWERE 740000000"  
irb(main):004:0\> line2 =~ /23430000.\*CodeRed/  
=\> nil

Regards,  
Yaser Sulaiman

> **···**
>
> On Mon, Dec 1, 2008 at 10:08 PM, Mmcolli00 Mom \<mmc\_collins@yahoo.com\>wrote:
> 
> > Hi everyone.
> > 
> > Just a question about regualar expression in Ruby. Is there a way to  
> > check in each line of a document for always beginning with "23430000"  
> > and somewhere on that same line another string 'CodeRed'?
> > 
> > line1: 23430000 @#$#$3455000CodeRed 24AAWERE 740000000
> > 
> > This is what I have so far when I import each textdata from another  
> > file.
> > 
> > textdata.should =~ /23430000/ |CodeRed/
> > 
> > ...the pipe was supposed to determine if CodeRed exists somewhere after  
> > the identity code 23430000. However, it doesn't work like this. Also for  
> > my code I am not using OR logic with it.  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [1 December 2008 19:37 UTC](https://rubytalk.org/t/regular-expressions/50458/3 "2008-12-01T19:37:22Z")

</div>

> Hi everyone.

Hello.

> Just a question about regualar expression in Ruby. Is there a way to  
> check in each line of a document for always beginning with "23430000"  
> and somewhere on that same line another string 'CodeRed'?

Sure.

> line1: 23430000 @#$#$3455000CodeRed 24AAWERE 740000000
> 
> This is what I have so far when I import each textdata from another  
> file.
> 
> textdata.should =~ /23430000/ |CodeRed/

&nbsp;&nbsp;&nbsp;textdata.should =~ /\A23430000.\*CodeRed/

My changes are simple:

\* \A is a regex atom the only matches at the beginning of the input. I used this to make sure 23430000 is at the beginning of the line and not later.  
\* .\* matches zero or more of pretty much anything. Newlines are the only character excluded. Thus this allows anything to appear between the number and CodeRed.

Now the regex above really just checks one line. If you want to check all lines, you'll want something like:

&nbsp;&nbsp;&nbsp;textdata.each do |line|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line.should =~ /\A23430000.\*CodeRed/  
&nbsp;&nbsp;&nbsp;end

Hope that helps.

James Edward Gray II

> **···**
>
> On Dec 1, 2008, at 1:08 PM, Mmcolli00 Mom wrote:

---

<div class="post-metadata">

**Author:** ![Kyle\_Schmitt](https://avatars.discourse-cdn.com/v4/letter/k/9e8a1a/32.png) [@Kyle\_Schmitt](https://rubytalk.org/u/Kyle_Schmitt)\
**Post date:** [1 December 2008 19:50 UTC](https://rubytalk.org/t/regular-expressions/50458/4 "2008-12-01T19:50:25Z")

</div>

I just wanted to mention another way of combining regexes that may  
help you stay sane: union.

#You write each regex nice and simple like..  
startswith=/~23430000/  
codered=/CodeRed/

#Then combine them to a complex one  
combined\_regex=Regexp.union(startswith,codered)

When you've got to build up some large regular expressions, this can  
be a godsend, especially when revisiting code you haven't looked at in  
awhile.

--Kyle

> **···**
>
> On Mon, Dec 1, 2008 at 1:08 PM, Mmcolli00 Mom \<mmc\_collins@yahoo.com\> wrote:
> 
> > Hi everyone.
> > 
> > Just a question about regualar expression in Ruby. Is there a way to  
> > check in each line of a document for always beginning with "23430000"  
> > and somewhere on that same line another string 'CodeRed'?
> > 
> > line1: 23430000 @#$#$3455000CodeRed 24AAWERE 740000000
> > 
> > This is what I have so far when I import each textdata from another  
> > file.
> > 
> > textdata.should =~ /23430000/ |CodeRed/
> > 
> > ...the pipe was supposed to determine if CodeRed exists somewhere after  
> > the identity code 23430000. However, it doesn't work like this. Also for  
> > my code I am not using OR logic with it.  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Richard\_Conroy1](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@Richard\_Conroy1](https://rubytalk.org/u/Richard_Conroy1)\
**Post date:** [4 December 2008 00:00 UTC](https://rubytalk.org/t/regular-expressions/50458/5 "2008-12-04T00:00:53Z")

</div>

I just simply have to pimp [http://www.rubular.com/](http://www.rubular.com/)

I don't think that I have closed that browser tab containing it in weeks.  
Its killer feature is the ability to supply your own test data.

> **[Rubular](https://rubular.com/)**
>
> Ruby-based regular expression editor/tester

/^23430000.\*CodeRed.\*$/

> **···**
>
> On Mon, Dec 1, 2008 at 7:08 PM, Mmcolli00 Mom \<mmc\_collins@yahoo.com\> wrote:
> 
> > Hi everyone.
> > 
> > Just a question about regualar expression in Ruby. Is there a way to  
> > check in each line of a document for always beginning with "23430000"  
> > and somewhere on that same line another string 'CodeRed'?
> > 
> > line1: 23430000 @#$#$3455000CodeRed 24AAWERE 740000000
> > 
> > This is what I have so far when I import each textdata from another  
> > file.
> > 
> > textdata.should =~ /23430000/ |CodeRed/
> > 
> > ...the pipe was supposed to determine if CodeRed exists somewhere after  
> > the identity code 23430000. However, it doesn't work like this. Also for  
> > my code I am not using OR logic with it.

---

<div class="post-metadata">

**Author:** ![Kyle\_Schmitt](https://avatars.discourse-cdn.com/v4/letter/k/9e8a1a/32.png) [@Kyle\_Schmitt](https://rubytalk.org/u/Kyle_Schmitt)\
**Post date:** [1 December 2008 19:52 UTC](https://rubytalk.org/t/regular-expressions/50458/6 "2008-12-01T19:52:11Z")

</div>

Scratch that, not thinking clearly! This is to match startswith OR  
codered, not necessarily both.

Still, I maintain that this is a way of staying sane with complex regexes 🙂

> **···**
>
> On Mon, Dec 1, 2008 at 1:51 PM, Kyle Schmitt \<kyleaschmitt@gmail.com\> wrote:
> 
> > I just wanted to mention another way of combining regexes that may  
> > help you stay sane: union.
> > 
> > #You write each regex nice and simple like..  
> > startswith=/~23430000/  
> > codered=/CodeRed/
> > 
> > #Then combine them to a complex one  
> > combined\_regex=Regexp.union(startswith,codered)
> > 
> > When you've got to build up some large regular expressions, this can  
> > be a godsend, especially when revisiting code you haven't looked at in  
> > awhile.
> > 
> > --Kyle

---

<div class="post-metadata">

**Author:** ![Mmcolli00\_Mom](https://avatars.discourse-cdn.com/v4/letter/m/76d3ee/32.png) [@Mmcolli00\_Mom](https://rubytalk.org/u/Mmcolli00_Mom)\
**Post date:** [1 December 2008 20:18 UTC](https://rubytalk.org/t/regular-expressions/50458/7 "2008-12-01T20:18:55Z")

</div>

thanks everyone!

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Yaser\_Sulaiman](https://avatars.discourse-cdn.com/v4/letter/y/7c8e57/32.png) [@Yaser\_Sulaiman](https://rubytalk.org/u/Yaser_Sulaiman)\
**Post date:** [1 December 2008 20:13 UTC](https://rubytalk.org/t/regular-expressions/50458/8 "2008-12-01T20:13:03Z")

</div>

> Still, I maintain that this is a way of staying sane with complex regexes

🙂

> 

It certainly looks helpful. I didn't know about it before. Thanks for  
sharing 🙂

Regards,  
Yaser

> **···**
>
> On Mon, Dec 1, 2008 at 10:52 PM, Kyle Schmitt \<kyleaschmitt@gmail.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Mmcolli00\_Mom](https://avatars.discourse-cdn.com/v4/letter/m/76d3ee/32.png) [@Mmcolli00\_Mom](https://rubytalk.org/u/Mmcolli00_Mom)\
**Post date:** [1 December 2008 20:17 UTC](https://rubytalk.org/t/regular-expressions/50458/9 "2008-12-01T20:17:36Z")

</div>

Kyle Schmitt wrote:

> **···**
>
> > On Mon, Dec 1, 2008 at 1:51 PM, Kyle Schmitt \<kyleaschmitt@gmail.com\> \> wrote:
> > 
> > > When you've got to build up some large regular expressions, this can  
> > > be a godsend, especially when revisiting code you haven't looked at in  
> > > awhile.
> > > 
> > > --Kyle
> > 
> > Scratch that, not thinking clearly! This is to match startswith OR  
> > codered, not necessarily both.
> > 
> > Still, I maintain that this is a way of staying sane with complex  
> > regexes 🙂
> 
> Alright. thanks for the tip. I was just thinking...what is the  
> regexpression for starts with anyway? I don't know..I figured maybe I  
> could use union with the starts with expression and then just grab that  
> value.  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Joe\_Wolfel](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@Joe\_Wolfel](https://rubytalk.org/u/Joe_Wolfel)\
**Post date:** [1 December 2008 21:32 UTC](https://rubytalk.org/t/regular-expressions/50458/10 "2008-12-01T21:32:38Z")

</div>

Interesting that there is a union function but no intersection function.

> **···**
>
> On 1 déc. 08, at 14:52, Kyle Schmitt wrote:
> 
> > On Mon, Dec 1, 2008 at 1:51 PM, Kyle Schmitt \> \<kyleaschmitt@gmail.com\> wrote:
> > 
> > > I just wanted to mention another way of combining regexes that may  
> > > help you stay sane: union.
> > > 
> > > #You write each regex nice and simple like..  
> > > startswith=/~23430000/  
> > > codered=/CodeRed/
> > > 
> > > #Then combine them to a complex one  
> > > combined\_regex=Regexp.union(startswith,codered)
> > > 
> > > When you've got to build up some large regular expressions, this can  
> > > be a godsend, especially when revisiting code you haven't looked at in  
> > > awhile.
> > > 
> > > --Kyle
> > 
> > Scratch that, not thinking clearly! This is to match startswith OR  
> > codered, not necessarily both.
> > 
> > Still, I maintain that this is a way of staying sane with complex regexes 🙂

---

<div class="post-metadata">

**Author:** ![Rob\_Biedenharn1](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@Rob\_Biedenharn1](https://rubytalk.org/u/Rob_Biedenharn1)\
**Post date:** [1 December 2008 22:08 UTC](https://rubytalk.org/t/regular-expressions/50458/11 "2008-12-01T22:08:16Z")

</div>

How would you even define a regexp (re) that matched only when both of two other regexps (re1, re2) matched?

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class Regexp  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def self.intersection(re1,re2)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;union(compile(/(?\>#{re1}).\*#{re2}/),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;compile(/(?\>#{re2}).\*#{re1}/))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;re = Regexp.intersection(re1,re2)

What would you expect the value to be? And while Regexp.union is well-behaved for multiple arguments, the expansion for more arguments in the intersection gets ugly fast.

-Rob

Rob Biedenharn [http://agileconsultingllc.com](http://agileconsultingllc.com)  
Rob@AgileConsultingLLC.com

> **···**
>
> On Dec 1, 2008, at 4:32 PM, Joe Wölfel wrote:
> 
> > On 1 déc. 08, at 14:52, Kyle Schmitt wrote:
> > 
> > > On Mon, Dec 1, 2008 at 1:51 PM, Kyle Schmitt \>\> \<kyleaschmitt@gmail.com\> wrote:
> > > 
> > > > I just wanted to mention another way of combining regexes that may  
> > > > help you stay sane: union.
> > > > 
> > > > #You write each regex nice and simple like..  
> > > > startswith=/~23430000/  
> > > > codered=/CodeRed/
> > > > 
> > > > #Then combine them to a complex one  
> > > > combined\_regex=Regexp.union(startswith,codered)
> > > > 
> > > > When you've got to build up some large regular expressions, this can  
> > > > be a godsend, especially when revisiting code you haven't looked at in  
> > > > awhile.
> > > > 
> > > > --Kyle
> > > 
> > > Scratch that, not thinking clearly! This is to match startswith OR  
> > > codered, not necessarily both.
> > > 
> > > Still, I maintain that this is a way of staying sane with complex regexes 🙂
> > 
> > Interesting that there is a union function but no intersection function.

---

<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:** [1 December 2008 22:08 UTC](https://rubytalk.org/t/regular-expressions/50458/12 "2008-12-01T22:08:50Z")

</div>

Joe Wölfel wrote:

> Interesting that there is a union function but no intersection function [for

regexen].

Well, the union of two regexen /foo/ and /bar/ is simply /foo|bar/, so the  
union method is rather easily implemented. An intersection method would be  
somewhat more complex. Of course that's not really a reason not to implement  
it, but it might be the reason why it's not implemented yet.

HTH,  
Sebastian

> **···**
>
> --  
> Jabber: sepp2k@jabber.org  
> ICQ: 205544826

---

<div class="post-metadata">

**Author:** ![Joe\_Wolfel](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@Joe\_Wolfel](https://rubytalk.org/u/Joe_Wolfel)\
**Post date:** [1 December 2008 22:41 UTC](https://rubytalk.org/t/regular-expressions/50458/13 "2008-12-01T22:41:31Z")

</div>

Not sure I understand. Are you arguing that an intersection cannot exist as a regular expression or merely that it is hard?

> **···**
>
> On 1 déc. 08, at 17:08, Rob Biedenharn wrote:
> 
> > On Dec 1, 2008, at 4:32 PM, Joe Wölfel wrote:
> > 
> > > On 1 déc. 08, at 14:52, Kyle Schmitt wrote:
> > > 
> > > > On Mon, Dec 1, 2008 at 1:51 PM, Kyle Schmitt \>\>\> \<kyleaschmitt@gmail.com\> wrote:
> > > > 
> > > > > I just wanted to mention another way of combining regexes that may  
> > > > > help you stay sane: union.
> > > > > 
> > > > > #You write each regex nice and simple like..  
> > > > > startswith=/~23430000/  
> > > > > codered=/CodeRed/
> > > > > 
> > > > > #Then combine them to a complex one  
> > > > > combined\_regex=Regexp.union(startswith,codered)
> > > > > 
> > > > > When you've got to build up some large regular expressions, this can  
> > > > > be a godsend, especially when revisiting code you haven't looked at in  
> > > > > awhile.
> > > > > 
> > > > > --Kyle
> > > > 
> > > > Scratch that, not thinking clearly! This is to match startswith OR  
> > > > codered, not necessarily both.
> > > > 
> > > > Still, I maintain that this is a way of staying sane with complex regexes 🙂
> > > 
> > > Interesting that there is a union function but no intersection function.
> > 
> > How would you even define a regexp (re) that matched only when both of two other regexps (re1, re2) matched?
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;class Regexp  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def self.intersection(re1,re2)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;union(compile(/(?\>#{re1}).\*#{re2}/),  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;compile(/(?\>#{re2}).\*#{re1}/))  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;re = Regexp.intersection(re1,re2)
> > 
> > What would you expect the value to be? And while Regexp.union is well-behaved for multiple arguments, the expansion for more arguments in the intersection gets ugly fast.
> > 
> > -Rob
> > 
> > Rob Biedenharn [http://agileconsultingllc.com](http://agileconsultingllc.com)  
> > Rob@AgileConsultingLLC.com

---

<div class="post-metadata">

**Author:** ![Ken\_Bloom](https://avatars.discourse-cdn.com/v4/letter/k/f08c70/32.png) [@Ken\_Bloom](https://rubytalk.org/u/Ken_Bloom)\
**Post date:** [2 December 2008 01:34 UTC](https://rubytalk.org/t/regular-expressions/50458/14 "2008-12-02T01:34:30Z")

</div>

It's very well defined if you're talking about an underlying  
deterministic finite automaton. That's an simple proof that is usually  
assigned as an exercise for the student in a computer science theory  
course.

If re1 compiles to a DFA dfa1=(S1,s1,A1,f1)  
where S1 is the set of all states, s1 is the start state, A1 is the set  
of accepting states, and f1(s'1,input) is the transition function  
and re2 compiles to a DFA dfa2=(S2,s2,A2,f2)

Then the intersection of these two languages can be recognized by the DFA  
dfa3=(S1 x S2, (s1,s2), A1 x A2, f3)  
where x means the cartesian product, and  
f3((s'1,s'2),input)=(f1(s'1,input),f2(s'2,input))

Now, how you'd turn that back into a regexp is not so easy...  
(but still doable)

--Ken

> **···**
>
> On Mon, 01 Dec 2008 17:08:16 -0500, Rob Biedenharn wrote:
> 
> > On Dec 1, 2008, at 4:32 PM, Joe Wölfel wrote:
> > 
> > > On 1 déc. 08, at 14:52, Kyle Schmitt wrote:
> > > 
> > > > On Mon, Dec 1, 2008 at 1:51 PM, Kyle Schmitt \<kyleaschmitt@gmail.com\> \>\>\> wrote:
> > > > 
> > > > > I just wanted to mention another way of combining regexes that may  
> > > > > help you stay sane: union.
> > > > > 
> > > > > #You write each regex nice and simple like.. startswith=/~23430000/  
> > > > > codered=/CodeRed/
> > > > > 
> > > > > #Then combine them to a complex one  
> > > > > combined\_regex=Regexp.union(startswith,codered)
> > > > > 
> > > > > When you've got to build up some large regular expressions, this can  
> > > > > be a godsend, especially when revisiting code you haven't looked at  
> > > > > in  
> > > > > awhile.
> > > > > 
> > > > > --Kyle
> > > > 
> > > > Scratch that, not thinking clearly! This is to match startswith OR  
> > > > codered, not necessarily both.
> > > > 
> > > > Still, I maintain that this is a way of staying sane with complex  
> > > > regexes 🙂
> > > 
> > > Interesting that there is a union function but no intersection  
> > > function.
> > 
> > How would you even define a regexp (re) that matched only when both of  
> > two other regexps (re1, re2) matched?
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class Regexp  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def self.intersection(re1,re2)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;union(compile(/(?\>#{re1}).\*#{re2}/),  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;compile(/(?\>#{re2}).\*#{re1}/))  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;re = Regexp.intersection(re1,re2)
> > 
> > What would you expect the value to be? And while Regexp.union is well-  
> > behaved for multiple arguments, the expansion for more arguments in the  
> > intersection gets ugly fast.
> 
> --  
> Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.  
> Department of Computer Science. Illinois Institute of Technology.  
> [http://www.iit.edu/~kbloom1/](http://www.iit.edu/~kbloom1/)

---

<div class="post-metadata">

**Author:** ![Rob\_Biedenharn1](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@Rob\_Biedenharn1](https://rubytalk.org/u/Rob_Biedenharn1)\
**Post date:** [1 December 2008 22:58 UTC](https://rubytalk.org/t/regular-expressions/50458/15 "2008-12-01T22:58:37Z")

</div>

That it becomes combinatorially hard to construct such a regexp in general. If I want a regexp that matches the intersection of /a/ and /b/ and /c/ (i.e., contains each of 'a', 'b', and 'c'), I have to account for all the permutations (manually):  
/a.\*b.\*c/  
/a.\*c.\*b/  
/b.\*a.\*c/  
/b.\*c.\*a/  
/c.\*a.\*b/  
/c.\*b.\*a/

Or combined as: /(?:a.\*(?:b.\*c)|(?:c.\*b))|(?:b.\*(?:a.\*c)|(?:c.\*a))|(?:c.\*(?:b.\*a)|(?:a.\*b))/

That's nasty and so much worse than the union /[abc]/ or /a|b|c/ even for this relatively simple case. It would be better to do this at the application level if you can't guarantee order:

[/a/, /b/, /c/].all? {|re| mystring =~ re }

And then the value of the match can be whatever the application wants to track.

-Rob

> **···**
>
> On Dec 1, 2008, at 5:41 PM, Joe Wölfel wrote:
> 
> > On 1 déc. 08, at 17:08, Rob Biedenharn wrote:
> > 
> > > On Dec 1, 2008, at 4:32 PM, Joe Wölfel wrote:
> > > 
> > > > On 1 déc. 08, at 14:52, Kyle Schmitt wrote:
> > > > 
> > > > > On Mon, Dec 1, 2008 at 1:51 PM, Kyle Schmitt \<kyleaschmitt@gmail.com \>\>\>\> \> wrote:
> > > > > 
> > > > > > I just wanted to mention another way of combining regexes that may  
> > > > > > help you stay sane: union.
> > > > > > 
> > > > > > #You write each regex nice and simple like..  
> > > > > > startswith=/~23430000/  
> > > > > > codered=/CodeRed/
> > > > > > 
> > > > > > #Then combine them to a complex one  
> > > > > > combined\_regex=Regexp.union(startswith,codered)
> > > > > > 
> > > > > > When you've got to build up some large regular expressions, this can  
> > > > > > be a godsend, especially when revisiting code you haven't looked at in  
> > > > > > awhile.
> > > > > > 
> > > > > > --Kyle
> > > > > 
> > > > > Scratch that, not thinking clearly! This is to match startswith OR  
> > > > > codered, not necessarily both.
> > > > > 
> > > > > Still, I maintain that this is a way of staying sane with complex regexes 🙂
> > > > 
> > > > Interesting that there is a union function but no intersection function.
> > > 
> > > How would you even define a regexp (re) that matched only when both of two other regexps (re1, re2) matched?
> > > 
> > > &nbsp;&nbsp;&nbsp;class Regexp  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def self.intersection(re1,re2)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;union(compile(/(?\>#{re1}).\*#{re2}/),  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;compile(/(?\>#{re2}).\*#{re1}/))  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > > &nbsp;&nbsp;&nbsp;end
> > > 
> > > &nbsp;&nbsp;&nbsp;re = Regexp.intersection(re1,re2)
> > > 
> > > What would you expect the value to be? And while Regexp.union is well-behaved for multiple arguments, the expansion for more arguments in the intersection gets ugly fast.
> > > 
> > > -Rob
> > > 
> > > Rob Biedenharn [http://agileconsultingllc.com](http://agileconsultingllc.com)  
> > > Rob@AgileConsultingLLC.com
> > 
> > Not sure I understand. Are you arguing that an intersection cannot exist as a regular expression or merely that it is hard?
