# Simple pattern matching

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.

On 3/27/06, Minkoo S. [email protected] wrote:

‘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

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("[^"]")/

\$1

\$2

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

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

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

Minkoo S. wrote:

‘node: { title: “15597629” label: “up[0,18 p:-2270.125]” }’.
split(’"’).values_at(1,3)

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.