# Ruby regexpresion

**URL:** <https://www.ruby-forum.com/t/ruby-regexpresion/195154>\
**Category:** Ruby\
**Created:** [September 17, 2010, 8:39pm UTC](https://www.ruby-forum.com/t/ruby-regexpresion/195154 "2010-09-17T20:39:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![woow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@woow](https://www.ruby-forum.com/u/woow)\
**Post date:** [September 17, 2010, 8:39pm UTC](https://www.ruby-forum.com/t/ruby-regexpresion/195154/1 "2010-09-17T20:39:41Z")

</div>

(21:15:32:873) SMART\>\> Starting Process ‘/abs/nc/qwcy/xyz’. pid 11560

I want to parse the following few things from the above string

(21:15:32:873)  
xyz  
pid 11560

and output as below,

(21:15:32:873), xyz, pid 11560

I am stuck and I would appreciate if someone could give me some help. I  
am newbie to ruby…thanks

---

<div class="post-metadata">

**Author:** ![woow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@woow](https://www.ruby-forum.com/u/woow)\
**Post date:** [September 17, 2010, 9:20pm UTC](https://www.ruby-forum.com/t/ruby-regexpresion/195154/2 "2010-09-17T21:20:48Z")

</div>

Ruwan B. wrote:

> (21:15:32:873) SMART\>\> Starting Process ‘/abs/nc/qwcy/xyz’. pid 11560
> 
> I want to parse the following few things from the above string
> 
> (21:15:32:873)  
> xyz  
> pid 11560
> 
> and output as below,
> 
> (21:15:32:873), xyz, pid 11560
> 
> I am stuck and I would appreciate if someone could give me some help. I  
> am newbie to ruby…thanks

[http://www.ruby-doc.org/docs/ProgrammingRuby/](http://www.ruby-doc.org/docs/ProgrammingRuby/) is a good start. Read the  
bits about regular expressions.

You can match the line with a regular expression, and use capture groups  
(in parentheses) to pick out the bits you want.

> > str = “hello/to/you”  
> > =\> “hello/to/you”  
> > str =~ %r{(._)/(._)}  
> > =\> 0  
> > $1  
> > =\> “hello/to”  
> > $2  
> > =\> “you”

---

<div class="post-metadata">

**Author:** ![woow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@woow](https://www.ruby-forum.com/u/woow)\
**Post date:** [September 17, 2010, 9:27pm UTC](https://www.ruby-forum.com/t/ruby-regexpresion/195154/3 "2010-09-17T21:27:50Z")

</div>

On 9/17/2010 1:39 PM, Ruwan B. wrote:

> (21:15:32:873), xyz, pid 11560
> 
> I am stuck and I would appreciate if someone could give me some help. I  
> am newbie to ruby…thanks

The following example will work for your singular example, but it’s  
likely that it won’t work for your most general case since it assumes a  
fair bit about the contents of the lines to parse. You’ll need to read  
up on regular expressions in general if you need to edit this.  
Fortunately, there are plenty of decent references online:

> **[Ruby Regexp Class - Regular Expressions in Ruby](https://www.regular-expressions.info/ruby.html)**
>
> Learn how to use regular expressions with Ruby and the Ruby Regexp class

> **[Ruby - Regular Expressions](http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm)**
>
> Ruby Regular Expressions - A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings using a specialized syntax held in a pattern.

> **[Ruby | zenspider.com | by ryan davis](https://www.zenspider.com/ruby/)**
>
> Ruby facts, opinions, source code, links, and stuff.

or just google for ruby regexp. 🙂

Example:

# Assume str holds the string you want to parse.

str = …

str =~ %r{((._))._/(._)'…(._)}  
puts [$1, $2, $3].join(', ')

This regexp uses the parenthesis around the first field of interest as  
the first marker and captures the text within and including the  
parenthesis. Then it chews up everything following that until the last  
/, which is the second marker. It captures the following text until the  
', the fourth marker. Finally it throws away the next 2 characters and  
captures the rest of the string.

It has captured 3 matches which become available in the special  
variables $1, $2, and $3 based on the order in which they were matched  
in the string. For convenience, those are put into an array, joined  
with ', ', and then printed out.

HTH. 🙂

-Jeremy

---

<div class="post-metadata">

**Author:** ![woow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@woow](https://www.ruby-forum.com/u/woow)\
**Post date:** [September 17, 2010, 10:10pm UTC](https://www.ruby-forum.com/t/ruby-regexpresion/195154/4 "2010-09-17T22:10:43Z")

</div>

Ruwan B. wrote:

> (21:15:32:873) SMART\>\> Starting Process ‘/abs/nc/qwcy/xyz’. pid 11560
> 
> I want to parse the following few things from the above string
> 
> (21:15:32:873)  
> xyz  
> pid 11560
> 
> and output as below,
> 
> (21:15:32:873), xyz, pid 11560
> 
> I am stuck and I would appreciate if someone could give me some help. I  
> am newbie to ruby…thanks

another answer is

s = “(21:15:32:873) SMART\>\> Starting Process ‘/abs/nc/qwcy/xyz’. pid  
11560”  
s =~ /^((._?))._’/._?/._?/._?/(._?)’._(pid \d_)/  
puts $1  
puts $2  
puts $3

the parentheses that capture the text are call “capturing parentheses”.

so the regular expression /what(._)is(._)this/ would sucessfully match  
the string “what mamamea this is crazy this” and the first captured  
group would be " mamamea " and the second captured group would be "  
crazy ".

Hopefully that makes sense.

---

<div class="post-metadata">

**Author:** ![woow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@woow](https://www.ruby-forum.com/u/woow)\
**Post date:** [September 17, 2010, 10:13pm UTC](https://www.ruby-forum.com/t/ruby-regexpresion/195154/5 "2010-09-17T22:13:45Z")

</div>

sorry, I lied, the first captured group would be " mamamea this "

---

<div class="post-metadata">

**Author:** ![woow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@woow](https://www.ruby-forum.com/u/woow)\
**Post date:** [September 17, 2010, 9:50pm UTC](https://www.ruby-forum.com/t/ruby-regexpresion/195154/6 "2010-09-17T21:50:33Z")

</div>

Jeremy thanks so much for your help…it works.

could you please explain further how %r{((._))._/(._)'…(._)}

captures the 3 interested part from the string…

I know you have explained but if you could kindly explain more with an  
easy way I think it will help me to learn…

thanks again

Jeremy B. wrote:

> On 9/17/2010 1:39 PM, Ruwan B. wrote:
> 
> > (21:15:32:873), xyz, pid 11560
> > 
> > I am stuck and I would appreciate if someone could give me some help. I  
> > am newbie to ruby…thanks
> 
> The following example will work for your singular example, but it’s  
> likely that it won’t work for your most general case since it assumes a  
> fair bit about the contents of the lines to parse. You’ll need to read  
> up on regular expressions in general if you need to edit this.  
> Fortunately, there are plenty of decent references online:
> 
> [Ruby Regexp Class - Regular Expressions in Ruby](http://www.regular-expressions.info/ruby.html)  
> [Ruby - Regular Expressions](http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm)  
> [http://www.zenspider.com/Languages/Ruby/QuickRef.html#12](http://www.zenspider.com/Languages/Ruby/QuickRef.html#12)  
> or just google for ruby regexp. 🙂
> 
> Example:
> 
> # Assume str holds the string you want to parse.
> 
> str = …
> 
> str =~ %r{((._))._/(._)'…(._)}  
> puts [$1, $2, $3].join(', ')
> 
> This regexp uses the parenthesis around the first field of interest as  
> the first marker and captures the text within and including the  
> parenthesis. Then it chews up everything following that until the last  
> /, which is the second marker. It captures the following text until the  
> ', the fourth marker. Finally it throws away the next 2 characters and  
> captures the rest of the string.
> 
> It has captured 3 matches which become available in the special  
> variables $1, $2, and $3 based on the order in which they were matched  
> in the string. For convenience, those are put into an array, joined  
> with ', ', and then printed out.
> 
> HTH. 🙂
> 
> -Jeremy

---

<div class="post-metadata">

**Author:** ![woow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@woow](https://www.ruby-forum.com/u/woow)\
**Post date:** [September 17, 2010, 10:23pm UTC](https://www.ruby-forum.com/t/ruby-regexpresion/195154/7 "2010-09-17T22:23:26Z")

</div>

On 9/17/2010 2:50 PM, Ruwan B. wrote:

> Jeremy thanks so much for your help…it works.
> 
> could you please explain further how %r{((._))._/(._)’…(._)}
> 
> captures the 3 interested part from the string…
> 
> I know you have explained but if you could kindly explain more with an  
> easy way I think it will help me to learn…
> 
> thanks again

Sure thing. Especially when you’re learning about regular expressions  
it really helps to start by breaking them down. You often have to do  
that even when you’re experienced but looking at someone else’s work.  
😉 Be aware that I’m going to gloss over quite a bit here, so you  
still need to learn some basics.

We can break down this regexp into 5 parts:  
((._)) \<- The first capture  
._/ \<- Noise  
(._) \<- The second capture  
'… \<- More noise  
(._) \<- The last capture

Anything within the unescaped parenthesis will be captured and assigned  
to one of the $1, $2, etc. global variables. In the first capture, we  
need to capture the literal parenthesis which appear in the string, so  
we escape these with backslashes in order to remove their special  
meaning to the regular expression engine. Then we want to slurp up any  
text between the parenthesis, so we use “.\*”.

The first bit of noise absorbs the uninteresting bits of the string  
which follow up to and including the forward slash. The \* operator is  
greedy and dot (.) matches any single character, so .\* will match  
everything including forward slashes. It only stops because we  
specifically said that this chunk of the regexp must end with a forward  
slash. Since it would be impossible to consume the last forward slash  
and still match one more, this noise accumulation stops. It’s not  
within parenthesis, so it’s not captured either.

The second capture cooperates with the following noise section. The  
capture part would greedily consume everything much like the first noise  
section; however, the second noise section requires that a single quote  
be available to start it. There must be at least 1 single quote left in  
the string, so the second capture stops when there is only 1 left in  
this case.

The second noise section goes on to consume 2 more characters after the  
single quote. This is because of the dot dot (…) in it. Each matches  
1 of any single character.

Finally the last capture section is left to consume the rest of the  
string exactly as the second capture would have done had nothing else  
followed it in the regexp.

When building regexps, it’s almost always a good idea to start small and  
build up. I like using irb for this since it gives me rapid results.  
You’ll need more basics than I can provide here though if you want to  
become proficient. Good luck!

-Jeremy
