# Pattern Matching Question

**URL:** <https://www.ruby-forum.com/t/pattern-matching-question/62279>\
**Category:** Ruby\
**Created:** [June 2, 2006, 3:48am UTC](https://www.ruby-forum.com/t/pattern-matching-question/62279 "2006-06-02T03:48:04Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mitchell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e19adc/32.png) [@mitchell](https://www.ruby-forum.com/u/mitchell)\
**Post date:** [June 2, 2006, 3:48am UTC](https://www.ruby-forum.com/t/pattern-matching-question/62279/1 "2006-06-02T03:48:04Z")

</div>

If I had the text “{ hello \} there }”, what regex could I use to get  
“hello } there” back? I’m really stumped on this one.

---

<div class="post-metadata">

**Author:** ![mitchell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e19adc/32.png) [@mitchell](https://www.ruby-forum.com/u/mitchell)\
**Post date:** [June 2, 2006, 4:46am UTC](https://www.ruby-forum.com/t/pattern-matching-question/62279/2 "2006-06-02T04:46:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mitchell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e19adc/32.png) [@mitchell](https://www.ruby-forum.com/u/mitchell)\
**Post date:** [June 2, 2006, 5:02am UTC](https://www.ruby-forum.com/t/pattern-matching-question/62279/3 "2006-06-02T05:02:15Z")

</div>

On 6/1/06, mitchell [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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”

---

<div class="post-metadata">

**Author:** ![mitchell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e19adc/32.png) [@mitchell](https://www.ruby-forum.com/u/mitchell)\
**Post date:** [June 3, 2006, 6:12am UTC](https://www.ruby-forum.com/t/pattern-matching-question/62279/4 "2006-06-03T06:12:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mitchell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e19adc/32.png) [@mitchell](https://www.ruby-forum.com/u/mitchell)\
**Post date:** [June 3, 2006, 4:31am UTC](https://www.ruby-forum.com/t/pattern-matching-question/62279/5 "2006-06-03T04:31:06Z")

</div>

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  
🙂

---

<div class="post-metadata">

**Author:** ![mitchell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e19adc/32.png) [@mitchell](https://www.ruby-forum.com/u/mitchell)\
**Post date:** [June 3, 2006, 9:40am UTC](https://www.ruby-forum.com/t/pattern-matching-question/62279/6 "2006-06-03T09:40:31Z")

</div>

2006/6/2, mitchell [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

---

<div class="post-metadata">

**Author:** ![mitchell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e19adc/32.png) [@mitchell](https://www.ruby-forum.com/u/mitchell)\
**Post date:** [June 4, 2006, 11:22am UTC](https://www.ruby-forum.com/t/pattern-matching-question/62279/7 "2006-06-04T11:22:41Z")

</div>

2006/6/3, mitchell [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

---

<div class="post-metadata">

**Author:** ![mitchell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e19adc/32.png) [@mitchell](https://www.ruby-forum.com/u/mitchell)\
**Post date:** [June 3, 2006, 4:05pm UTC](https://www.ruby-forum.com/t/pattern-matching-question/62279/8 "2006-06-03T16:05:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mitchell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e19adc/32.png) [@mitchell](https://www.ruby-forum.com/u/mitchell)\
**Post date:** [June 4, 2006, 9:20pm UTC](https://www.ruby-forum.com/t/pattern-matching-question/62279/9 "2006-06-04T21:20:04Z")

</div>

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
