Simple pattern matching


#1

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.


#2

On 3/27/06, Minkoo S. 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 :wink:

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

#3

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]””]


#4

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]”]


#5

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