To extract a particular string

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

On 2013-09-18, at 6:49 AM, Raj pal [email protected] 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 S. [email protected]
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.

On Wed, Sep 18, 2013 at 12:49 PM, Raj pal [email protected] 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.

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.

hi Mike S.,

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

RAJ

hi Juan P. Avello

Thank you.

hi Mike S. and Juan P. 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?

On 2013-09-18, at 7:07 AM, Raj pal [email protected] wrote:

hi Mike S.,

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 S. [email protected]
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.

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?

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 S.,

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

On 2013-09-18, at 7:20 AM, Raj pal [email protected] wrote:

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

[how] are you,I am [fine]

RAJ

You can read the Class: Regexp (Ruby 2.0.0), 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 S. [email protected]
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.

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?

On Sep 18, 2013, at 7:43 AM, Regis d’Aubarede [email protected]
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

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 …

hi tamouse m
Thank you for your code,it’s working fine too.

hi Robert K.

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

On Thu, Sep 19, 2013 at 7:59 AM, Tamara T. [email protected]
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

On Thu, Sep 19, 2013 at 10:56 AM, Raj pal [email protected] 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

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?

hi Robert K.

Ohhhhhhhhhhhhhhhhhh, OK, I haven’t noticed that. Thank you.

hi Jesús Gabriel y Galán,

Yes I understood. Thank you.