Simple pattern matching

M

Minkoo Seo

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

Robert Dober

------=_Part_5148_25995024.1143479278611
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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.




'node: { title: "15597629" label: "up[0,18 p:-2270.125]" }' =3D~
%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=EAtise humaine ; en ce qui
concerne l'univers, je n'en ai pas acquis la certitude absolue.

- Albert Einstein

------=_Part_5148_25995024.1143479278611--
 
R

Ross Bamford

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]\""]
 
W

William James

Minkoo said:
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.

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

=> ["15597629", "up[0,18 p:-2270.125]"]
 
W

why the lucky stiff

Minkoo said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top