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

···

--
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

···

On 2013-09-18, at 6:49 AM, 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".

RAJ

--
Posted via http://www.ruby-forum.com/\.

--

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

The "`Stok' disclaimers" apply.

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

Jesus.

···

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".

hi Mike Stok,

   it's working. Thank you very much.

RAJ

···

--
Posted via http://www.ruby-forum.com/.

hi Juan Pablo Avello

Thank you.

···

--
Posted via http://www.ruby-forum.com/.

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?

···

--
Posted via http://www.ruby-forum.com/.

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

···

--
Posted via http://www.ruby-forum.com/.

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?

···

--
Posted via http://www.ruby-forum.com/.

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?

···

--
Posted via http://www.ruby-forum.com/.

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

···

--
Posted via http://www.ruby-forum.com/.

hi Robert Klemme

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

···

--
Posted via http://www.ruby-forum.com/.

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?

···

--
Posted via http://www.ruby-forum.com/.

hi Jesús Gabriel y Galán,

Yes I understood. Thank you.

···

--
Posted via http://www.ruby-forum.com/.

You should take Jesus’s 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

···

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/\.

--

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

The "`Stok' disclaimers" apply.

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

···

On 2013-09-18, at 7:20 AM, Raj pal <lists@ruby-forum.com> wrote:

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

--

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

The "`Stok' disclaimers" apply.

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

···

--
Posted via http://www.ruby-forum.com/\.

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

···

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.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

From the docs at: http://www.ruby-doc.org/core-2.0.0/String.html#method-i-5B-5D

"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.

···

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?

or

s.scan /\[(.*?)\]/ # don't be greedy

···

On Sep 18, 2013, at 7:43 AM, Regis d'Aubarede <lists@ruby-forum.com> wrote:

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

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 7:59 AM, Tamara Temple <tamouse.lists@gmail.com> wrote:

On Sep 18, 2013, at 7:43 AM, Regis d'Aubarede <lists@ruby-forum.com> wrote:

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

or

s.scan /\[(.*?)\]/ # don't be greedy

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/