# Regexp's comparison

**URL:** <https://www.ruby-forum.com/t/regexps-comparison/110559>\
**Category:** Ruby\
**Created:** [July 27, 2007, 2:28pm UTC](https://www.ruby-forum.com/t/regexps-comparison/110559 "2007-07-27T14:28:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [July 27, 2007, 2:28pm UTC](https://www.ruby-forum.com/t/regexps-comparison/110559/1 "2007-07-27T14:28:42Z")

</div>

Hi guys,

I have string i.e:  
a  
b  
c  
c  
c  
a  
c  
d

For example I would like to match all c lines with regexp. How can I  
create the regexp if c lines can be ordered differently i.e:  
a  
c  
b  
c  
c  
a  
d

I will be appreciated for any help

Thanks

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [July 27, 2007, 2:53pm UTC](https://www.ruby-forum.com/t/regexps-comparison/110559/2 "2007-07-27T14:53:53Z")

</div>

Marcin T. wrote:

> d
> 
> For example I would like to match all c lines with regexp. How can I  
> create the regexp if c lines can be ordered differently i.e:  
> a  
> c  
> b  
> c  
> c  
> a  
> d

Not entirely sure I understand the question. What are you expecting the  
output to be in each case?

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [July 27, 2007, 2:56pm UTC](https://www.ruby-forum.com/t/regexps-comparison/110559/3 "2007-07-27T14:56:53Z")

</div>

On 7/27/07, Marcin T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> d
> 
> I will be appreciated for any help
> 
> ## Thanks
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Marcin I have no idea what you want achieve? Do you have lines of  
strings or one string you have written into different lines?  
What is the output/result you want from the input?

My first wild guess is that you should look at Enumerable#grep.

R.

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [July 27, 2007, 3:03pm UTC](https://www.ruby-forum.com/t/regexps-comparison/110559/4 "2007-07-27T15:03:54Z")

</div>

On 7/27/07, Marcin T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> c  
> d  
> Do you want to find out how many lines have c? Or find out witch line  
> numbers c is on? Or do you have one string “acbccad” or “ascffbdrc” and  
> you  
> are trying to find out if c is in that line? If that is what you are  
> trying  
> to do then try this:

“acbccad”.scan(/c/)

can you tell us more about what you are looking for? Sorry if this  
hasn’t  
helped.

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [July 27, 2007, 3:14pm UTC](https://www.ruby-forum.com/t/regexps-comparison/110559/5 "2007-07-27T15:14:50Z")

</div>

Guys,  
I’m going to search for iptables entries for some IP’s. The worst thing  
is that rules in iptables can be each time in different order. I need to  
check whether all desired rules for particular IP are in iptables.  
Moreover I need to find out if there are no more entries than expected.

Assume that a, b, c, d etc… represent one line in iptable (one entry -  
a for IP = x.x.x.x, b for y.y.y.y etc). I need to check whether 3 (not  
only one and no more than 3) rules for ip x.x.x.x are in iptables but  
these rules (lines) each time may be in different positions (other rules  
may be placed between them etc…)

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [July 27, 2007, 3:27pm UTC](https://www.ruby-forum.com/t/regexps-comparison/110559/6 "2007-07-27T15:27:57Z")

</div>

On 7/27/07, Marcin T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Assume that a, b, c, d etc… represent one line in iptable (one entry -  
> a for IP = x.x.x.x, b for y.y.y.y etc). I need to check whether 3 (not  
> only one and no more than 3) rules for ip x.x.x.x are in iptables but  
> these rules (lines) each time may be in different positions (other rules  
> may be placed between them etc…)

You can do the scan with lenght, like this:

text = “adcdcbcdc”  
text.scan(/c/).length

this will return the total times c shows up in your string.

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [July 27, 2007, 4:02pm UTC](https://www.ruby-forum.com/t/regexps-comparison/110559/7 "2007-07-27T16:02:00Z")

</div>

2007/7/27, Marcin T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> may be placed between them etc…)  
> I would not do any sorting here. Rather I’d fill a Hash with IPs as  
> keys and arrays of rules as values. That way you do not have to  
> bother with ordering and also it’s faster (probably not an issue in  
> your case). If you just want to count rules then you only need a  
> counter as value.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [July 27, 2007, 3:30pm UTC](https://www.ruby-forum.com/t/regexps-comparison/110559/8 "2007-07-27T15:30:23Z")

</div>

Sam M. wrote:

> On 7/27/07, Marcin T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Assume that a, b, c, d etc… represent one line in iptable (one entry -  
> > a for IP = x.x.x.x, b for y.y.y.y etc). I need to check whether 3 (not  
> > only one and no more than 3) rules for ip x.x.x.x are in iptables but  
> > these rules (lines) each time may be in different positions (other rules  
> > may be placed between them etc…)
> 
> You can do the scan with lenght, like this:
> 
> text = “adcdcbcdc”  
> text.scan(/c/).length
> 
> this will return the total times c shows up in your string.

Yeah,  
I’ve found it. Thanks for help. I think this would solve all issues in  
my code.
