# A method to search and cut in Ruby

**URL:** <https://rubytalk.org/t/a-method-to-search-and-cut-in-ruby/39417>\
**Category:** ruby-talk\
**Created:** [25 July 2007 23:00 UTC](https://rubytalk.org/t/a-method-to-search-and-cut-in-ruby/39417 "2007-07-25T23:00:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Scomboni](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@Scomboni](https://rubytalk.org/u/Scomboni)\
**Post date:** [25 July 2007 23:00 UTC](https://rubytalk.org/t/a-method-to-search-and-cut-in-ruby/39417/1 "2007-07-25T23:00:10Z")

</div>

Hello all,  
I'm trying to parse some EXIF data and return certain fields of text.  
If I wanted to parsed a document looking for text and then locating it  
take everything after a colon : delimitator what is the best method in  
ruby vs Shell command?

For example if I have a file called filename.txt and I want to search  
for this ---\> "Compression : JPEG (old-style)"  
but only return everything to the right of the delimitator?

Using command line tool such as grep to locate and then cut to grab  
everything after " : " works but I'm trying to learn how to do things  
like this in Ruby and having no luck grabbing everything to the right  
of the colon. Some of the text I'm grabbing can contain additional  
colons and I want everything after the first one.

Thanks.

Sc--

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner3](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@Gavin\_Kistner3](https://rubytalk.org/u/Gavin_Kistner3)\
**Post date:** [25 July 2007 23:09 UTC](https://rubytalk.org/t/a-method-to-search-and-cut-in-ruby/39417/2 "2007-07-25T23:09:59Z")

</div>

C:\\>irb  
irb(main):001:0\> s = "Foo : Bar"  
=\> "Foo : Bar"  
irb(main):002:0\> s.split ":"  
=\> ["Foo ", " Bar"]  
irb(main):003:0\> s.split(":").last  
=\> " Bar"

...or did you want the leading whitespace chomped?

irb(main):004:0\> s.split( /\s\*:\s\*/ )  
=\> ["Foo", "Bar"]  
irb(main):005:0\> s.split( /\s\*:\s\*/ ).last  
=\> "Bar"

> **···**
>
> On Jul 25, 4:59 pm, scomb...@gmail.com wrote:
> 
> > For example if I have a file called filename.txt and I want to search  
> > for this ---\> "Compression : JPEG (old-style)"  
> > but only return everything to the right of the delimitator?

---

<div class="post-metadata">

**Author:** ![Scomboni](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@Scomboni](https://rubytalk.org/u/Scomboni)\
**Post date:** [26 July 2007 00:40 UTC](https://rubytalk.org/t/a-method-to-search-and-cut-in-ruby/39417/3 "2007-07-26T00:40:05Z")

</div>

I was looking at this method as well. I was having trouble though as I  
know the left hand side column so for this example the Compression  
portion then the colon : the remainder could be anything for instance  
a description with lots of text and could also contain a additional  
colon. At the shell prompt I could grep "Compression" | cut -d: -f2-  
and that would grab everything...  
So it sounds like I'm looking in the correct area but Im just not  
pulling it all together. I will keep at it if you have additional info  
to point to that would be great..and appreciated.  
Thanks again.

Sc-

> **···**
>
> On Jul 25, 7:05 pm, Phrogz \<phr...@mac.com\> wrote:
> 
> > On Jul 25, 4:59 pm, scomb...@gmail.com wrote:
> > 
> > \> For example if I have a file called filename.txt and I want to search  
> > \> for this ---\> "Compression : JPEG (old-style)"  
> > \> but only return everything to the right of the delimitator?
> > 
> > C:\\>irb  
> > irb(main):001:0\> s = "Foo : Bar"  
> > =\> "Foo : Bar"  
> > irb(main):002:0\> s.split ":"  
> > =\> ["Foo ", " Bar"]  
> > irb(main):003:0\> s.split(":").last  
> > =\> " Bar"
> > 
> > ...or did you want the leading whitespace chomped?
> > 
> > irb(main):004:0\> s.split( /\s\*:\s\*/ )  
> > =\> ["Foo", "Bar"]  
> > irb(main):005:0\> s.split( /\s\*:\s\*/ ).last  
> > =\> "Bar"

---

<div class="post-metadata">

**Author:** ![bbiker](https://avatars.discourse-cdn.com/v4/letter/b/97f17d/32.png) [@bbiker](https://rubytalk.org/u/bbiker)\
**Post date:** [26 July 2007 01:09 UTC](https://rubytalk.org/t/a-method-to-search-and-cut-in-ruby/39417/4 "2007-07-26T01:09:59Z")

</div>

Some of the text I'm grabbing can contain additional  
colons and I want everything after the first one.

SC  
I am assuming you want everything after the first colon but not after  
the second

irb(main):001:0\> line = "Compression : JPEG (old\_style) : whatever"  
=\> "Compression : JPEG (old\_style) : whatever"  
irb(main):002:0\> if line =~ /^Compression\s\*:?(.\*)$/  
irb(main):003:1\> end\_str = $1  
irb(main):004:1\> first\_el = end\_str.split(/:/).first  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts first\_el  
irb(main):005:1\> end  
JPEG (old\_style)

if you everything after each colon

el = end\_str.split(/:/)

p el = {"JPEG (old-style)", "whatever"]

> **···**
>
> On Jul 25, 7:05 pm, Phrogz \<phr...@mac.com\> wrote:
> 
> > On Jul 25, 4:59 pm, scomb...@gmail.com wrote:
> > 
> > \> For example if I have a file called filename.txt and I want to search  
> > \> for this ---\> "Compression : JPEG (old-style)"  
> > \> but only return everything to the right of the delimitator?
> > 
> > C:\\>irb  
> > irb(main):001:0\> s = "Foo : Bar"  
> > =\> "Foo : Bar"  
> > irb(main):002:0\> s.split ":"  
> > =\> ["Foo ", " Bar"]  
> > irb(main):003:0\> s.split(":").last  
> > =\> " Bar"
> > 
> > ...or did you want the leading whitespace chomped?
> > 
> > irb(main):004:0\> s.split( /\s\*:\s\*/ )  
> > =\> ["Foo", "Bar"]  
> > irb(main):005:0\> s.split( /\s\*:\s\*/ ).last  
> > =\> "Bar"

---

<div class="post-metadata">

**Author:** ![Phil4](https://avatars.discourse-cdn.com/v4/letter/p/45deac/32.png) [@Phil4](https://rubytalk.org/u/Phil4)\
**Post date:** [26 July 2007 07:29 UTC](https://rubytalk.org/t/a-method-to-search-and-cut-in-ruby/39417/5 "2007-07-26T07:29:59Z")

</div>

scomboni@gmail.com schrieb:  
...

> > ...or did you want the leading whitespace chomped?
> > 
> > irb(main):004:0\> s.split( /\s\*:\s\*/ )  
> > =\> ["Foo", "Bar"]  
> > irb(main):005:0\> s.split( /\s\*:\s\*/ ).last  
> > =\> "Bar"
> 
> I was looking at this method as well. I was having trouble though as I  
> know the left hand side column so for this example the Compression  
> portion then the colon : the remainder could be anything for instance  
> a description with lots of text and could also contain a additional  
> colon. At the shell prompt I could grep "Compression" | cut -d: -f2-  
> and that would grab everything...

To get everything after the first colon you can use:  
s =~ /^.\*?😕  
textAfterColon = $'.dup

To also get rid of the spaces directly after the first colon use this Regex:  
s =~ /^.\*?:\s\*/  
textAfterColon = $'.dup

The trick is to anchor the regex (i.e. using ^)

BR Phil

> **···**
>
> > On Jul 25, 7:05 pm, Phrogz \<phr...@mac.com\> wrote:
> > 
> > > On Jul 25, 4:59 pm, scomb...@gmail.com wrote:

---

<div class="post-metadata">

**Author:** ![Thomas\_Gantner](https://avatars.discourse-cdn.com/v4/letter/t/53a042/32.png) [@Thomas\_Gantner](https://rubytalk.org/u/Thomas_Gantner)\
**Post date:** [26 July 2007 23:00 UTC](https://rubytalk.org/t/a-method-to-search-and-cut-in-ruby/39417/6 "2007-07-26T23:00:00Z")

</div>

split takes an optional second parameter \<limit\>, defining how many elements  
the resuting array shall have at most

'foo : bar : baz'.split(/\s\*:\s\*/, 2)  
=\> ["foo", "bar : baz"]

-Thomas

> **···**
>
> on Thu 26. July 2007 02.37, scomboni@gmail.com wrote:
> 
> > On Jul 25, 7:05 pm, Phrogz \<phr...@mac.com\> wrote:
> > 
> > > On Jul 25, 4:59 pm, scomb...@gmail.com wrote:
> > > 
> > > \> For example if I have a file called filename.txt and I want to search  
> > > \> for this ---\> "Compression : JPEG (old-style)"  
> > > \> but only return everything to the right of the delimitator?
> > > 
> > > C:\\>irb  
> > > irb(main):001:0\> s = "Foo : Bar"  
> > > =\> "Foo : Bar"  
> > > irb(main):002:0\> s.split ":"  
> > > =\> ["Foo ", " Bar"]  
> > > irb(main):003:0\> s.split(":").last  
> > > =\> " Bar"
> > > 
> > > ...or did you want the leading whitespace chomped?
> > > 
> > > irb(main):004:0\> s.split( /\s\*:\s\*/ )  
> > > =\> ["Foo", "Bar"]  
> > > irb(main):005:0\> s.split( /\s\*:\s\*/ ).last  
> > > =\> "Bar"
> > 
> > I was looking at this method as well. I was having trouble though as I  
> > know the left hand side column so for this example the Compression  
> > portion then the colon : the remainder could be anything for instance  
> > a description with lots of text and could also contain a additional  
> > colon. At the shell prompt I could grep "Compression" | cut -d: -f2-  
> > and that would grab everything...  
> > So it sounds like I'm looking in the correct area but Im just not  
> > pulling it all together. I will keep at it if you have additional info  
> > to point to that would be great..and appreciated.  
> > Thanks again.
> 
> --  
> \<sig. under construction\>

---

<div class="post-metadata">

**Author:** ![Scomboni](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@Scomboni](https://rubytalk.org/u/Scomboni)\
**Post date:** [26 July 2007 11:15 UTC](https://rubytalk.org/t/a-method-to-search-and-cut-in-ruby/39417/7 "2007-07-26T11:15:01Z")

</div>

Thanks so much for everyones answers much appreciated. The space after  
the colon, I thought I was going to have to live with that. Thanks  
again for the help.  
Scott

> **···**
>
> On Jul 26, 3:28 am, Phil Meier \<phil.me...@gmx.net\> wrote:
> 
> > scomb...@gmail.com schrieb:
> > 
> > \> On Jul 25, 7:05 pm, Phrogz \<phr...@mac.com\> wrote:  
> > \>\> On Jul 25, 4:59 pm, scomb...@gmail.com wrote:  
> > ...  
> > \>\> ...or did you want the leading whitespace chomped?
> > 
> > \>\> irb(main):004:0\> s.split( /\s\*:\s\*/ )  
> > \>\> =\> ["Foo", "Bar"]  
> > \>\> irb(main):005:0\> s.split( /\s\*:\s\*/ ).last  
> > \>\> =\> "Bar"
> > 
> > \> I was looking at this method as well. I was having trouble though as I  
> > \> know the left hand side column so for this example the Compression  
> > \> portion then the colon : the remainder could be anything for instance  
> > \> a description with lots of text and could also contain a additional  
> > \> colon. At the shell prompt I could grep "Compression" | cut -d: -f2-  
> > \> and that would grab everything...
> > 
> > To get everything after the first colon you can use:  
> > s =~ /^.\*?😕  
> > textAfterColon = $'.dup
> > 
> > To also get rid of the spaces directly after the first colon use this Regex:  
> > s =~ /^.\*?:\s\*/  
> > textAfterColon = $'.dup
> > 
> > The trick is to anchor the regex (i.e. using ^)
> > 
> > BR Phil
