If I had the text “{ hello \} there }”, what regex could I use to get
“hello } there” back? I’m really stumped on this one.
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
On 6/1/06, mitchell [email protected] 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”
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
Dave B. 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
2006/6/2, mitchell [email protected]:
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
2006/6/3, mitchell [email protected]:
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
Jeff P. 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 middleOr 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.
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