Forum: Ruby Pattern Matching Question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D695ecbcb6de95b36f1dfe6685650ee2?d=identicon&s=25 mitchell (Guest)
on 2006-06-02 03:48
If I had the text "{ hello \\} there }", what regex could I use to get
"hello } there" back? I'm really stumped on this one.
0b561a629b87f0bbf71b45ee5a48febb?d=identicon&s=25 Dave Burt (Guest)
on 2006-06-02 04:46
(Received via mailing list)
mitchell wrote:
> If I had the text "{ hello \\} there }", what regex could I use to get
> "hello } there" back? I'm really stumped on this one.

I'm not sure exactly what you're trying to do, but is this close?

s = "{ hello \\} there }"
a = s.scan(/ \{ (( \\ \} | [^}] )*) \} /x).map {|match| match[0].strip }
a[0].gsub!("\\}", "}")
p a[0] #=> "hello } there"

Cheers,
Dave
Dabdaf107905a3605174b18ead82d45d?d=identicon&s=25 Jason Sweat (sweatje)
on 2006-06-02 05:02
(Received via mailing list)
On 6/1/06, mitchell <fs8206@yahoo.com> wrote:
> If I had the text "{ hello \\} there }", what regex could I use to get
> "hello } there" back? I'm really stumped on this one.

with the new regex engine in ruby 1.9 you can use a lookbehind
assertion:

$ ruby19 -e 'p /\{(((?<=\\)\}|[^}])*)\}/.match("{ hello \\} there
}")[1].strip.gsub("\\}","}")'
"hello } there"
3eda6fcd3204ef285fa52176c28c4d3e?d=identicon&s=25 mitchell (Guest)
on 2006-06-03 04:31
Dave Burt wrote:
> mitchell wrote:
>> If I had the text "{ hello \\} there }", what regex could I use to get
>> "hello } there" back? I'm really stumped on this one.
>
> I'm not sure exactly what you're trying to do, but is this close?
>
> s = "{ hello \\} there }"
> a = s.scan(/ \{ (( \\ \} | [^}] )*) \} /x).map {|match| match[0].strip }
> a[0].gsub!("\\}", "}")
> p a[0] #=> "hello } there"
>
> Cheers,
> Dave

Cool! I didn't think about the | option. That's quite ingenius. Thanks
:)
8dad1ec4d769734583f45fbbee5cd009?d=identicon&s=25 Jeff Pritchard (Guest)
on 2006-06-03 06:12
mitchell wrote:
> If I had the text "{ hello \\} there }", what regex could I use to get
> "hello } there" back? I'm really stumped on this one.

Hi Mitchell,
I'm puzzled by the simplicity of your question.  Does it tell the whole
story?  If so, you could simplify the whole thing down to:

a, b = input.scan(/\w+/)   # find the two words - there are always two
words, right?
output = "#{a} } #{b}"         # output the two words with a space brace
space in the middle


Or maybe it is always just like you show it, and all you want to do is
remove the '\\' and the outer braces and spaces, in which case it is as
simple as:

output = input[2..-3].gsub!(%r{\\},'')

Just trying to suggest that with a pattern matching question one needs a
bit more context in order to know how simple or complicated the matching
needs to be.  But then, maybe if you told us you would have to shoot us.
:)

best,
jp
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2006-06-03 09:40
(Received via mailing list)
2006/6/2, mitchell <fs8206@yahoo.com>:
> If I had the text "{ hello \\} there }", what regex could I use to get
> "hello } there" back? I'm really stumped on this one.

I guess you are trying to extract this string from a larger one.  Try
this:

s.scan %r<\{((?:\\.|[^\\}])*)\}> do puts $1.strip.gsub(/\\(.)/, '\\1')
end

or - without stripping the white space:

s.scan %r<\{((?:\\.|[^\\}])*)\}> do puts $1.gsub(/\\(.)/, '\\1') end

HTH

robert
3eda6fcd3204ef285fa52176c28c4d3e?d=identicon&s=25 mitchell (Guest)
on 2006-06-03 16:05
Jeff Pritchard wrote:
> mitchell wrote:
>> If I had the text "{ hello \\} there }", what regex could I use to get
>> "hello } there" back? I'm really stumped on this one.
>
> Hi Mitchell,
> I'm puzzled by the simplicity of your question.  Does it tell the whole
> story?  If so, you could simplify the whole thing down to:
>
> a, b = input.scan(/\w+/)   # find the two words - there are always two
> words, right?
> output = "#{a} } #{b}"         # output the two words with a space brace
> space in the middle
>
>
> Or maybe it is always just like you show it, and all you want to do is
> remove the '\\' and the outer braces and spaces, in which case it is as
> simple as:
>
> output = input[2..-3].gsub!(%r{\\},'')
>
> Just trying to suggest that with a pattern matching question one needs a
> bit more context in order to know how simple or complicated the matching
> needs to be.  But then, maybe if you told us you would have to shoot us.
> :)
>
> best,
> jp

Actually that was just a simple example. The big idea is to be able to
escape '}'s in any inputted string. I later figured that replacing any
'\}' with some value (such as '}'s hex code) would work, but playing
with regex sometimes can be enlightening :) Thanks for the input though.
Much appreciated.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2006-06-04 11:22
(Received via mailing list)
2006/6/3, mitchell <none@none.com>:
> Actually that was just a simple example. The big idea is to be able to
> escape '}'s in any inputted string. I later figured that replacing any
> '\}' with some value (such as '}'s hex code) would work, but playing
> with regex sometimes can be enlightening :) Thanks for the input though.
> Much appreciated.

For the one character delimiter situation there is a better solution
than replacement (see my last posting).

If you're interested to dive into this topic I recommend "Mastering
Regular Expressions" (O'Reilly).  It's a really good book which covers
building correct and also fast regular expressions and is well worth
the money.

Kind regards

robert
Ffcb418e17cac2873d611c2b8d8d891c?d=identicon&s=25 Benjohn Barnes (Guest)
on 2006-06-04 21:20
(Received via mailing list)
On 3 Jun 2006, at 15:06, mitchell wrote:
> Actually that was just a simple example. The big idea is to be able to
> escape '}'s in any inputted string. I later figured that replacing any
> '\}' with some value (such as '}'s hex code) would work, but playing
> with regex sometimes can be enlightening :) Thanks for the input
> though.
> Much appreciated.

I think I have the wrong end of the stick here, but is Regexp::escape
any use to you?

Cheers,
	Benjohn
This topic is locked and can not be replied to.