# Reg exp

**URL:** <https://www.ruby-forum.com/t/reg-exp/154309>\
**Category:** Ruby\
**Created:** [November 27, 2008, 3:33pm UTC](https://www.ruby-forum.com/t/reg-exp/154309 "2008-11-27T15:33:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hai\_l](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c68b51/32.png) [@hai\_l](https://www.ruby-forum.com/u/hai_l)\
**Post date:** [November 27, 2008, 3:33pm UTC](https://www.ruby-forum.com/t/reg-exp/154309/1 "2008-11-27T15:33:47Z")

</div>

Hi every body. I’m a newbie in Ruby and have problem with reg exp.

In one string as example “The device 13c include something as in Fig. 2a  
and 3b”, I want to detect some numbers have style “13c” and ignore  
others number have style “2a and 3b”

I use /(\d+[a-z]_)( [^a][^n][^d] )(\d)+[a-z]_/ but it not ok. Can  
someone help me plz?

---

<div class="post-metadata">

**Author:** ![hai\_l](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c68b51/32.png) [@hai\_l](https://www.ruby-forum.com/u/hai_l)\
**Post date:** [November 27, 2008, 3:45pm UTC](https://www.ruby-forum.com/t/reg-exp/154309/2 "2008-11-27T15:45:52Z")

</div>

It would be helpful if you would share several examples of patterns that  
you  
want to match and patterns that you want to exclude. Also,  
[http://www.rubular.com](http://www.rubular.com) is a great place for experimenting with regular  
expressions in Ruby.

Regards,  
Craig

---

<div class="post-metadata">

**Author:** ![hai\_l](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c68b51/32.png) [@hai\_l](https://www.ruby-forum.com/u/hai_l)\
**Post date:** [November 27, 2008, 3:49pm UTC](https://www.ruby-forum.com/t/reg-exp/154309/3 "2008-11-27T15:49:27Z")

</div>

Hai anh Le wrote:

> Hi every body. I’m a newbie in Ruby and have problem with reg exp.
> 
> In one string as example “The device 13c include something as in Fig. 2a  
> and 3b”, I want to detect some numbers have style “13c” and ignore  
> others number have style “2a and 3b”
> 
> I use /(\d+[a-z]_)( [^a][^n][^d] )(\d)+[a-z]_/ but it not ok. Can  
> someone help me plz?

Tricky. I suggest:

str.scan(/\d+[a-z]?(?: and \d+[a-z]?)?/).reject { |e| /and/ =~ e }

I thought about lookahead assertions, e.g. (?! and \d+), but since these  
don’t consume characters the 3b is likely to be matched later as a  
standalone number.

Details at [Programming Ruby: The Pragmatic Programmer's Guide](http://www.ruby-doc.org/docs/ProgrammingRuby/) - click on “The  
Ruby Language” in the contents, and scroll down to Extensions.

---

<div class="post-metadata">

**Author:** ![hai\_l](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c68b51/32.png) [@hai\_l](https://www.ruby-forum.com/u/hai_l)\
**Post date:** [November 27, 2008, 4:29pm UTC](https://www.ruby-forum.com/t/reg-exp/154309/4 "2008-11-27T16:29:34Z")

</div>

Brian C. wrote:

> Hai anh Le wrote:
> 
> > Hi every body. I’m a newbie in Ruby and have problem with reg exp.
> > 
> > In one string as example “The device 13c include something as in Fig. 2a  
> > and 3b”, I want to detect some numbers have style “13c” and ignore  
> > others number have style “2a and 3b”
> > 
> > I use /(\d+[a-z]_)( [^a][^n][^d] )(\d)+[a-z]_/ but it not ok. Can  
> > someone help me plz?
> 
> Tricky. I suggest:
> 
> str.scan(/\d+[a-z]?(?: and \d+[a-z]?)?/).reject { |e| /and/ =~ e }
> 
> I thought about lookahead assertions, e.g. (?! and \d+), but since these  
> don’t consume characters the 3b is likely to be matched later as a  
> standalone number.
> 
> Details at [Programming Ruby: The Pragmatic Programmer's Guide](http://www.ruby-doc.org/docs/ProgrammingRuby/) - click on “The  
> Ruby Language” in the contents, and scroll down to Extensions.

Hi Brian!

I have another example as:

“The coefficients are then mixed with the data from mixer 51xy, so that  
the recording equipment 14abc stores the six channel data stream, the  
scripting data 26aa and 27ac”

My purpose is to find the position of number have style “123abc”, after  
that I get the string from begin to that position like “The coefficients  
are then mixed with the data from mixer 51xy,”  
Then I will continued search…

My code like

while string.size do  
if string=~ /…/ then  
temp\_string = $` + $&  
string.gsub!(temp\_string, “”)  
else  
break  
end

So that what I need is $& = “123abc” and not = “123abc and 123abc”

---

<div class="post-metadata">

**Author:** ![hai\_l](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c68b51/32.png) [@hai\_l](https://www.ruby-forum.com/u/hai_l)\
**Post date:** [November 27, 2008, 6:00pm UTC](https://www.ruby-forum.com/t/reg-exp/154309/5 "2008-11-27T18:00:42Z")

</div>

Hai anh Le wrote:

> I have another example as:
> 
> “The coefficients are then mixed with the data from mixer 51xy, so that  
> the recording equipment 14abc stores the six channel data stream, the  
> scripting data 26aa and 27ac”
> 
> My purpose is to find the position of number have style “123abc”, after  
> that I get the string from begin to that position like “The coefficients  
> are then mixed with the data from mixer 51xy,”  
> Then I will continued search…
> 
> My code like
> 
> while string.size do  
> if string=~ /…/ then  
> temp\_string = $` + $&  
> string.gsub!(temp\_string, “”)  
> else  
> break  
> end
> 
> So that what I need is $& = “123abc” and not = “123abc and 123abc”

Sorry - that still does not make it clear what you want, especially with  
the portion of the string " … the scripting data 26aa and 27ac"

Here is some code:

str = \<\<EOS  
The coefficients are then mixed with the data from mixer 51xy, so that  
the recording equipment 14abc stores the six channel data stream, the  
scripting data 26aa and 27ac  
EOS

p str.scan(/._?\d+[a-z]_/m)

# [“The coefficients are then mixed with the data from mixer 51xy”, ",

so that \nthe recording equipment 14abc", " stores the six channel data  
stream, the \nscripting data 26aa", " and 27ac"]

If you want to keep 26aa but not 27ac, then use reject to strip out the  
match " and 27ac".

Here is another option:

p str.scan(/._?\d+[a-z]_(?: and \d+[a-z]\*)?/m)

# [“The coefficients are then mixed with the data from mixer 51xy”, ",

so that \nthe recording equipment 14abc", " stores the six channel data  
stream, the \nscripting data 26aa and 27ac"]

This time the whole clause up to an including 26aa and 27ac is a  
separate item, and you can strip this out using reject.

---

<div class="post-metadata">

**Author:** ![hai\_l](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c68b51/32.png) [@hai\_l](https://www.ruby-forum.com/u/hai_l)\
**Post date:** [November 28, 2008, 2:34am UTC](https://www.ruby-forum.com/t/reg-exp/154309/6 "2008-11-28T02:34:54Z")

</div>

p str.scan(/._?\d+[a-z]_(?: and \d+[a-z]\*)?/m)

# [“The coefficients are then mixed with the data from mixer 51xy”, ",

so that the recording equipment 14abc", " stores the six channel data  
stream, the scripting data 26aa and 27ac"]

yes, it is all thing I want. Thank you very much
