[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Simple pattern matching

minkoo.seo@gmail.com

3/27/2006 4:56:00 PM

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.

4 Answers

Robert Dober

3/27/2006 5:08:00 PM

0

On 3/27/06, Minkoo Seo <minkoo.seo@gmail.com> 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.
>
> 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

Ross Bamford

3/27/2006 5:17:00 PM

0

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

--
Ross Bamford - rosco@roscopeco.REMOVE.co.uk



William James

3/27/2006 5:20:00 PM

0

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.
>
> 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]"]

why the lucky stiff

3/27/2006 5:26:00 PM

0

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