Forum: Ruby Simple pattern matching

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
47b36de21d7ecbc824c81d24802a6290?d=identicon&s=25 Minkoo Seo (pool007)
on 2006-03-27 18:59
(Received via mailing list)
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.
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2006-03-27 19:08
(Received via mailing list)
On 3/27/06, Minkoo Seo <minkoo.seo@gmail.com> 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
A9b6a93b860020caf9d2d1d58c32478f?d=identicon&s=25 Ross Bamford (Guest)
on 2006-03-27 19:17
(Received via mailing list)
On Tue, 2006-03-28 at 01:58 +0900, Minkoo Seo 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]\""]
2ee1a7960cc761a6e92efb5000c0f2c9?d=identicon&s=25 William James (Guest)
on 2006-03-27 19:24
(Received via mailing list)
Minkoo Seo 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]"]
4674615d2cf231975c741731be9a8685?d=identicon&s=25 why the lucky stiff (Guest)
on 2006-03-27 19:25
(Received via mailing list)
Minkoo Seo 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 topic is locked and can not be replied to.