[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

My regexp stupidity needs assistance before loose all my hair!

T. Onoma

1/17/2005 9:21:00 PM

Let me painfully honest: I hate parsing, especially w/ regexp, and I don't
care if it's because I stupid and suck at it. It shouldn't have to be this
hair pulling! Anyway... Can some one please give the regular expression to
match the first square bracket's contents. In this case it would be "Hello".

s = <<-EOS
[Hello]
This [b]is[b.] a test.
[Hello.]
EOS

Much obliged,
T.


21 Answers

Zach Dennis

1/17/2005 9:26:00 PM

0

trans. (T. Onoma) wrote:
> Let me painfully honest: I hate parsing, especially w/ regexp, and I don't
> care if it's because I stupid and suck at it. It shouldn't have to be this
> hair pulling! Anyway... Can some one please give the regular expression to
> match the first square bracket's contents. In this case it would be "Hello".
>
> s = <<-EOS
> [Hello]
> This [b]is[b.] a test.
> [Hello.]
> EOS

The trick here is to make sure you are non-greedy.

s =~ /\[([^\]]*)\]/

Zach





Douglas Livingstone

1/17/2005 9:27:00 PM

0

I think that this is what you need: /\[[\w]+\]/

This little application might help you (not sure if it is 100% Ruby
compatible, but may be a start) called TestRexp, which you can get
here: http://regexpstudio.com/RegExpS...

hth,
Douglas


On Tue, 18 Jan 2005 06:20:43 +0900, trans. (T. Onoma)
<transami@runbox.com> wrote:
> Let me painfully honest: I hate parsing, especially w/ regexp, and I don't
> care if it's because I stupid and suck at it. It shouldn't have to be this
> hair pulling! Anyway... Can some one please give the regular expression to
> match the first square bracket's contents. In this case it would be "Hello".
>
> s = <<-EOS
> [Hello]
> This [b]is[b.] a test.
> [Hello.]
> EOS
>
> Much obliged,
> T.
>
>


Zach Dennis

1/17/2005 9:27:00 PM

0

Zach Dennis wrote:
> trans. (T. Onoma) wrote:
>
>> Let me painfully honest: I hate parsing, especially w/ regexp, and I
>> don't care if it's because I stupid and suck at it. It shouldn't have
>> to be this hair pulling! Anyway... Can some one please give the
>> regular expression to match the first square bracket's contents. In
>> this case it would be "Hello".
>>
>> s = <<-EOS
>> [Hello]
>> This [b]is[b.] a test.
>> [Hello.]
>> EOS
>
>
> The trick here is to make sure you are non-greedy.
>
> s =~ /\[([^\]]*)\]/

Almost forgot, $1 is the match you are looking for.

> Zach


Zach Dennis

1/17/2005 9:29:00 PM

0

Douglas Livingstone wrote:
> I think that this is what you need: /\[[\w]+\]/

Ah, this is nicer and shorter then mine... I think I will use this one
to. =)

Zach


Glenn Parker

1/17/2005 9:31:00 PM

0

trans. (T. Onoma) wrote:
> Let me painfully honest: I hate parsing, especially w/ regexp, and I don't
> care if it's because I stupid and suck at it. It shouldn't have to be this
> hair pulling! Anyway... Can some one please give the regular expression to
> match the first square bracket's contents. In this case it would be "Hello".
>
> s = <<-EOS
> [Hello]
> This [b]is[b.] a test.
> [Hello.]
> EOS

s =~ /\[([^\]]*)\]/
puts $1

--
Glenn Parker | glenn.parker-AT-comcast.net | <http://www.tetrafoi...


T. Onoma

1/17/2005 9:41:00 PM

0

On Monday 17 January 2005 04:26 pm, Zach Dennis wrote:
| trans. (T. Onoma) wrote:
| > Let me painfully honest: I hate parsing, especially w/ regexp, and I
| > don't care if it's because I stupid and suck at it. It shouldn't have to
| > be this hair pulling! Anyway... Can some one please give the regular
| > expression to match the first square bracket's contents. In this case it
| > would be "Hello".
| >
| > s = <<-EOS
| > [Hello]
| > This [b]is[b.] a test.
| > [Hello.]
| > EOS
|
| The trick here is to make sure you are non-greedy.
|
| s =~ /\[([^\]]*)\]/

Thanks. I _see_ now why mine wasn't working, though I don't _understand_ why
it wasn't working. I was using the / /x extension, because I generally like
to space the parts my regexps out to read easier, but for some reason that
causes the above to match [b] instead. Oh well, I just won't do that.

Thanks All for your responses!
T.


T. Onoma

1/17/2005 9:47:00 PM

0

On Monday 17 January 2005 04:40 pm, trans. (T. Onoma) wrote:
| On Monday 17 January 2005 04:26 pm, Zach Dennis wrote:
| | trans. (T. Onoma) wrote:
| | > Let me painfully honest: I hate parsing, especially w/ regexp, and I
| | > don't care if it's because I stupid and suck at it. It shouldn't have
| | > to be this hair pulling! Anyway... Can some one please give the regular
| | > expression to match the first square bracket's contents. In this case
| | > it would be "Hello".
| | >
| | > s = <<-EOS
| | > [Hello]
| | > This [b]is[b.] a test.
| | > [Hello.]
| | > EOS
| |
| | The trick here is to make sure you are non-greedy.
| |
| | s =~ /\[([^\]]*)\]/
|
| Thanks. I _see_ now why mine wasn't working, though I don't _understand_
| why it wasn't working. I was using the / /x extension, because I generally
| like to space the parts my regexps out to read easier, but for some reason
| that causes the above to match [b] instead. Oh well, I just won't do that.

Oops scratch that. That's not the reason either (sigh). But I got it working
now anyway. Thanks.

T.


Douglas Livingstone

1/17/2005 9:49:00 PM

0

On Tue, 18 Jan 2005 06:28:37 +0900, Zach Dennis <zdennis@mktec.com> wrote:
> Ah, this is nicer and shorter then mine... I think I will use this one
> to. =)

And I was thinking "ooh, Zach's looks like a better way to do it" :)

Douglas


Assaph Mehr

1/17/2005 9:50:00 PM

0

> Thanks. I _see_ now why mine wasn't working, though I don't
> _understand_ why it wasn't working. I was using the / /x extension,
> because I generally like to space the parts my regexps out to
> read easier, but for some reason that causes the above to
> match [b] instead. Oh well, I just won't do that.

It has todo with the pattern matching being greedy, not the /x flag.
your pattern will match a '[' then as many characters as possible -
including ']' - until a final closing ']'.
There are two solutions:
1. As shown, match any non ']'.
2. Make the match non greedy: %r{ \[(.+?)\] }x

HTH,
Assaph
ps. If you want all occurences in the string, use string#scan instead
of String#match.

Mark Hubbart

1/17/2005 10:01:00 PM

0

On Tue, 18 Jan 2005 06:26:08 +0900, Zach Dennis <zdennis@mktec.com> wrote:
> trans. (T. Onoma) wrote:
> > Let me painfully honest: I hate parsing, especially w/ regexp, and I don't
> > care if it's because I stupid and suck at it. It shouldn't have to be this
> > hair pulling! Anyway... Can some one please give the regular expression to
> > match the first square bracket's contents. In this case it would be "Hello".
> >
> > s = <<-EOS
> > [Hello]
> > This [b]is[b.] a test.
> > [Hello.]
> > EOS
>
> The trick here is to make sure you are non-greedy.
>
> s =~ /\[([^\]]*)\]/

Or:

s =~ /\[.*?\]/

which uses the ? non-greedy modifier to ensure that only the very next
"]" is matched. For example:


str = <<EOT
[this] [is a test]
here are[some]brackets
[brackets ]
[] no words
no brackets
EOT
==>"[this] [is a test]\nhere are[some]brackets\n[brackets ]\n[] no
words\nno brackets\n"

str.each{|line| p line.scan(/\[.*?\]/)}
["[this]", "[is a test]"]
["[some]"]
["[brackets ]"]
["[]"]
[]

cheers,
Mark