# Simple pattern matching

**URL:** <https://www.ruby-forum.com/t/simple-pattern-matching/56053>\
**Category:** Ruby\
**Created:** [March 27, 2006, 6:59pm UTC](https://www.ruby-forum.com/t/simple-pattern-matching/56053 "2006-03-27T18:59:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pool007](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pool007](https://www.ruby-forum.com/u/pool007)\
**Post date:** [March 27, 2006, 6:59pm UTC](https://www.ruby-forum.com/t/simple-pattern-matching/56053/1 "2006-03-27T18:59:06Z")

</div>

Hi group.

Sorry for newbie question. Given the line like

node: { title: “15597629” label: “up[0,18 p:-2270.125]” }

How can I parse the above string into “15597629” and “up[0,18  
p:-2270.125]”? There are numbers in the first “…” and a-z, :, [, and  
] in the second “…” part.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![pool007](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pool007](https://www.ruby-forum.com/u/pool007)\
**Post date:** [March 27, 2006, 7:08pm UTC](https://www.ruby-forum.com/t/simple-pattern-matching/56053/2 "2006-03-27T19:08:50Z")

</div>

On 3/27/06, Minkoo S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks in advance.

‘node: { title: “15597629” label: “up[0,18 p:-2270.125]” }’ =~  
%r{(“._?")._?(”.\*?")}

seems a reasonable way to do it. Move the quotes outside the () if you  
do  
not want them excluded  
from the results.  
Access the data with

$1, $2 or Regexp.last\_match[1], …[2]

Hope that helps  
Robert

## P.S. Regexp questions are never newbee 😉

Deux choses sont infinies : l’univers et la bÃªtise humaine ; en ce qui  
concerne l’univers, je n’en ai pas acquis la certitude absolue.

- Albert Einstein

---

<div class="post-metadata">

**Author:** ![pool007](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pool007](https://www.ruby-forum.com/u/pool007)\
**Post date:** [March 27, 2006, 7:17pm UTC](https://www.ruby-forum.com/t/simple-pattern-matching/56053/3 "2006-03-27T19:17:19Z")

</div>

On Tue, 2006-03-28 at 01:58 +0900, Minkoo S. wrote:

> Hi group.
> 
> Sorry for newbie question. Given the line like
> 
> node: { title: “15597629” label: “up[0,18 p:-2270.125]” }
> 
> How can I parse the above string into “15597629” and “up[0,18  
> p:-2270.125]”? There are numbers in the first “…” and a-z, :, [, and  
> ] in the second “…” part.

Here are a couple of ways:

s = ‘node: { title: “15597629” label: “up[0,18 p:-2270.125]” }’

# =\> “node: { title: “15597629” label: “up[0,18 p:-2270.125]” }”

s =~ /title:\s("[^"]_")\slabel:\s("[^"]_")/

# =\> 8

$1

# =\> "“15597629"”

$2

# =\> ““up[0,18 p:-2270.125]””

s.scan(/"[^"]\*"/)

# =\> ["“15597629"”, ““up[0,18 p:-2270.125]””]

---

<div class="post-metadata">

**Author:** ![pool007](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pool007](https://www.ruby-forum.com/u/pool007)\
**Post date:** [March 27, 2006, 7:24pm UTC](https://www.ruby-forum.com/t/simple-pattern-matching/56053/4 "2006-03-27T19:24:28Z")

</div>

Minkoo S. wrote:

> Thanks in advance.  
> ‘node: { title: “15597629” label: “up[0,18 p:-2270.125]” }’.  
> split(’"’).values\_at(1,3)

=\> [“15597629”, “up[0,18 p:-2270.125]”]

---

<div class="post-metadata">

**Author:** ![pool007](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pool007](https://www.ruby-forum.com/u/pool007)\
**Post date:** [March 27, 2006, 7:25pm UTC](https://www.ruby-forum.com/t/simple-pattern-matching/56053/5 "2006-03-27T19:25:39Z")

</div>

Minkoo S. wrote:

> Sorry for newbie question. Given the line like
> 
> node: { title: “15597629” label: “up[0,18 p:-2270.125]” }
> 
> How can I parse the above string into “15597629” and “up[0,18  
> p:-2270.125]”? There are numbers in the first “…” and a-z, :, [, and  
> ] in the second “…” part.
> 
> > str = ‘node: { title: “15597629” label: “up[0,18 p:-2270.125]” }’  
> > node = YAML.load( str.sub(/"[^"]+"/, ‘\0,’) )  
> > node[‘title’]  
> > =\> “15597629”
> 
> > node[‘label’]  
> > =\> “up[0,18 p:-2270.125]”

I don’t know if it’s helpful, but it’s at least kind of striking.

\_why
