Forum: Ruby To extract a particular string

5dfc843f9ecf6fb64b52a35bdce43910?d=identicon&s=25 Raja gopalan (arc)
on 2013-09-18 12:49
hi

a="hi how [are] you"

Can I have the regular expression which returns the string which comes
under '[]'. In the above case it's "are".

RAJ
E088bb5c80fd3c4fd02c2020cdacbaf0?d=identicon&s=25 "Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> (Guest)
on 2013-09-18 13:00
(Received via mailing list)
On Wed, Sep 18, 2013 at 12:49 PM, Raj pal <lists@ruby-forum.com> wrote:
> hi
>
> a="hi how [are] you"
>
> Can I have the regular expression which returns the string which comes
> under '[]'. In the above case it's "are".

What have you tried?
You can use some tools to help you like www.rubular.com or regexpal.com

Jesus.
2ffac40f8a985a2b2749244b8a1c4161?d=identicon&s=25 Mike Stok (Guest)
on 2013-09-18 13:01
(Received via mailing list)
On 2013-09-18, at 6:49 AM, Raj pal <lists@ruby-forum.com> wrote:

> Posted via http://www.ruby-forum.com/.
One way to do it might be:

ratdog:~ mike$ pry
[1] pry(main)> a="hi how [are] you"
=> "hi how [are] you"
[2] pry(main)> /\[(.*)\]/.match a
=> #<MatchData "[are]" 1:"are">
[3] pry(main)> (/\[(.*)\]/.match a)[1]
=> "are"

Hope this helps,

Mike

--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
69112da010aa0192e775a4236cd9af3a?d=identicon&s=25 Juan Pablo Avello (Guest)
on 2013-09-18 13:03
(Received via mailing list)
El miércoles, 18 de septiembre de 2013 12:49:15 UTC+2, Raj pal escribió:
> --
> Posted via http://www.ruby-forum.com/.
>

There are several ways. One of them would be:

"hi how [are] you".scan(/\[(.*)\]/).flatten.first
=> "are"

Hope it helps.
5dfc843f9ecf6fb64b52a35bdce43910?d=identicon&s=25 Raja gopalan (arc)
on 2013-09-18 13:07
hi Mike Stok,

   it's working. This is what I wanted.Thank you very much.

RAJ
5dfc843f9ecf6fb64b52a35bdce43910?d=identicon&s=25 Raja gopalan (arc)
on 2013-09-18 13:10
hi Juan Pablo Avello

Thank you.
5dfc843f9ecf6fb64b52a35bdce43910?d=identicon&s=25 Raja gopalan (arc)
on 2013-09-18 13:12
hi Mike Stok and Juan Pablo Avello

I used your regular expression,it's working, Now I need to avoid the
second instance of [], for an example, "hi [are] you,I am [fine]". Here
I only need "are" and "fine" should be eliminated, How would I do that?
2ffac40f8a985a2b2749244b8a1c4161?d=identicon&s=25 Mike Stok (Guest)
on 2013-09-18 13:12
(Received via mailing list)
On 2013-09-18, at 7:07 AM, Raj pal <lists@ruby-forum.com> wrote:

> hi Mike Stok,
>
>   it's working. Thank you very much.
>
> RAJ
>
> --
> Posted via http://www.ruby-forum.com/.

You should take Jesuss advice and use a tool like rubular or regexpal to
make sure you understand what is happening, and test out whether it
works as you want for odd cases.

Consider a string like "this [is] a [test] string". In a case like that
do you want "is", "test", "is] a [test", ["is", "test"], or some kind of
error action to be the result?

Hope this helps,

Mike

--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
5dfc843f9ecf6fb64b52a35bdce43910?d=identicon&s=25 Raja gopalan (arc)
on 2013-09-18 13:20
hi Jesús Gabriel y Galán

I am sorry I haven't seen the comment of yours.yes i will check that
out.

hi Mike Stok,

Input :hi [how] are you,I am [fine]

My needed output would be : how

But when I use your regular expression it's returning like

[how] are you,I am [fine]

RAJ
5dfc843f9ecf6fb64b52a35bdce43910?d=identicon&s=25 Raja gopalan (arc)
on 2013-09-18 13:29
hi Jesús Gabriel y Galán

I tried with the tool which you mentioned. Thank you for that, But
unfortunately, i couldn't find extract the first instance of [] while
this [] appears second time like

hi how [are] [you]

Now it return this


1.  are] [you

I only need [are], how can i write to extract "are" alone?
2ffac40f8a985a2b2749244b8a1c4161?d=identicon&s=25 Mike Stok (Guest)
on 2013-09-18 13:35
(Received via mailing list)
On 2013-09-18, at 7:20 AM, Raj pal <lists@ruby-forum.com> wrote:

>
> But when I use your regular expression it's returning like
>
> [how] are you,I am [fine]
>
> RAJ

You can read the http://ruby-doc.org/core-2.0.0/Regexp.html, in the
Repetition section there is an example of using a following ? to change
the greedy operator into a lazy operator which matches as little as
possible.

See if you can apply it to the regular expression I gave you to produce
the right results.

A tool like http://www.rubular.com makes it easy to experiment and see
what is going on.

Hope this helps,

Mike


--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
B078cb4f4fb473c7a54d1fc36d10c70e?d=identicon&s=25 Regis d'Aubarede (raubarede)
on 2013-09-18 14:43
> hi how [are] [you]
>
> Now it return this
> 1.  are] [you


s="ssss[fff]gggg[ssss]ddddddd"

s.scan /\[(.*)\]/
=> [["fff]gggg[ssss"]]

Explanation: .* enlarge to maximum length ==> reach last bracket


s.scan /\[([^\]]*)\]/
=> [["fff"], ["ssss"]]

Explanation: [^\]]* enlarge to first bracket ...
5dfc843f9ecf6fb64b52a35bdce43910?d=identicon&s=25 Raja gopalan (arc)
on 2013-09-19 07:13
hi Regis d'Aubarede,

Excellent, This is what I needed.

I changed my code according to my need

puts line[/\[([^\]]*)\]/].delete('[]').delete('\'') unless
line[/\[([^\]]*)\]/].nil?
Aa082c8b00a50928e5860dcd70bf2368?d=identicon&s=25 tamouse m. (tamouse_m)
on 2013-09-19 08:00
(Received via mailing list)
On Sep 18, 2013, at 7:43 AM, Regis d'Aubarede <lists@ruby-forum.com>
wrote:

>
> Explanation: .* enlarge to maximum length ==> reach last bracket
>
>
> s.scan /\[([^\]]*)\]/
> => [["fff"], ["ssss"]]
>
> Explanation: [^\]]* enlarge to first bracket ...

or

s.scan /\[(.*?)\]/ # don't be greedy
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (robert_k78)
on 2013-09-19 09:15
(Received via mailing list)
On Thu, Sep 19, 2013 at 7:59 AM, Tamara Temple <tamouse.lists@gmail.com>
wrote:
>>
>
> or
>
> s.scan /\[(.*?)\]/ # don't be greedy

I think OP said he needs only the first match.  In that case he can do

irb(main):001:0> a="hi how [are] you"
=> "hi how [are] you"
irb(main):002:0> a[/\[([^\]]*)\]/, 1]
=> "are"
irb(main):003:0> a[/\[(.*?)\]/, 1]
=> "are"

Kind regards

robert
5dfc843f9ecf6fb64b52a35bdce43910?d=identicon&s=25 Raja gopalan (arc)
on 2013-09-19 10:56
hi  tamouse m
Thank you for your code,it's working fine too.

hi Robert Klemme

You have included the 1 in the array like a[/\[(.*?)\]/, 1], But
a[/\[(.*?)\]/] also works in the same way ,It also returns the first
occurrence of the string.

RAJ
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (robert_k78)
on 2013-09-19 13:12
(Received via mailing list)
On Thu, Sep 19, 2013 at 10:56 AM, Raj pal <lists@ruby-forum.com> wrote:

> You have included the 1 in the array like a[/\[(.*?)\]/, 1], But
> a[/\[(.*?)\]/] also works in the same way ,It also returns the first
> occurrence of the string.

I know.  But without the 1 you'll also get the brackets.

irb(main):001:0> "foo"[/f(o+)/]
=> "foo"
irb(main):002:0> "foo"[/f(o+)/, 1]
=> "oo"

And, btw, it's not an Array. It's operator String#[].

Cheers

robert
5dfc843f9ecf6fb64b52a35bdce43910?d=identicon&s=25 Raja gopalan (arc)
on 2013-09-19 13:27
hi Robert Klemme

Ohhhhhhhhhhhhhhhhhh, OK, I haven't noticed that. Thank you.
5dfc843f9ecf6fb64b52a35bdce43910?d=identicon&s=25 Raja gopalan (arc)
on 2013-09-19 20:01
Hi Robert,

Can you explain me why this 1 removes the Bracket? I refered the
document but it's not clear enough to understand. why it removes both
opening and closing bracket here?
E088bb5c80fd3c4fd02c2020cdacbaf0?d=identicon&s=25 "Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> (Guest)
on 2013-09-20 08:37
(Received via mailing list)
On Thu, Sep 19, 2013 at 8:01 PM, Raj pal <lists@ruby-forum.com> wrote:
> Hi Robert,
>
> Can you explain me why this 1 removes the Bracket? I refered the
> document but it's not clear enough to understand. why it removes both
> opening and closing bracket here?

From the docs at:
http://www.ruby-doc.org/core-2.0.0/String.html#met...

"If a Regexp is supplied, the matching portion of the string is
returned. If a capture follows the regular expression, which may be a
capture group index or name, follows the regular expression that
component of the MatchData is returned instead."

When you surround some part of the regex in parens, that's a capturing
group, and by passing the second argument as a 1, you are telling that
you are interested in the first captured group. You can also have
named groups, instead of by index:

a[/\[(?<word>[^\]]*)\]/, :word]

Hope this helps,

Jesus.
5dfc843f9ecf6fb64b52a35bdce43910?d=identicon&s=25 Raja gopalan (arc)
on 2013-09-21 06:34
hi Jesús Gabriel y Galán,

Yes I understood. Thank you.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.