regexp for $(text)

S

Szymon Drejewicz

I have string
a = "ala $(ela) ola $(tola) lola $(p_ola) ".

And I need some magic function that works this way:
["ela", "tola", "p_ola"]

How to do this?

Szymon
 
E

Emmanuel Touzery

Szymon said:
I have string
a = "ala $(ela) ola $(tola) lola $(p_ola) ".

And I need some magic function that works this way:
["ela", "tola", "p_ola"]

How to do this?

just escape the ( and the $.

irb(main):001:0> a = "ala $(ela) ola $(tola) lola $(p_ola) "
=> "ala $(ela) ola $(tola) lola $(p_ola) "
irb(main):002:0> a.scan /\$\((\w+)\)/
=> [["ela"], ["tola"], ["p_ola"]]
irb(main):003:0>

emmanuel
 
E

Emmanuel Touzery

Emmanuel said:
Szymon said:
I have string
a = "ala $(ela) ola $(tola) lola $(p_ola) ".

And I need some magic function that works this way:
puts a.params
["ela", "tola", "p_ola"]

How to do this?


just escape the ( and the $.

irb(main):001:0> a = "ala $(ela) ola $(tola) lola $(p_ola) "
=> "ala $(ela) ola $(tola) lola $(p_ola) "
irb(main):002:0> a.scan /\$\((\w+)\)/
=> [["ela"], ["tola"], ["p_ola"]]
irb(main):003:0>

emmanuel
btw if you just want a one-depth array:

irb(main):004:0> a.scan(/\$\((\w+)\)/).flatten
=> ["ela", "tola", "p_ola"]

emmanuel
 
R

Robert Klemme

Emmanuel Touzery said:
Emmanuel said:
Szymon said:
I have string
a = "ala $(ela) ola $(tola) lola $(p_ola) ".

And I need some magic function that works this way:

puts a.params
["ela", "tola", "p_ola"]

How to do this?


just escape the ( and the $.

irb(main):001:0> a = "ala $(ela) ola $(tola) lola $(p_ola) "
=> "ala $(ela) ola $(tola) lola $(p_ola) "
irb(main):002:0> a.scan /\$\((\w+)\)/
=> [["ela"], ["tola"], ["p_ola"]]
irb(main):003:0>

emmanuel
btw if you just want a one-depth array:

irb(main):004:0> a.scan(/\$\((\w+)\)/).flatten
=> ["ela", "tola", "p_ola"]

Also:

irb(main):010:0> a.scan(/\$\(([^)]*)\)/).map{|m| m[0]}
=> ["ela", "tola", "p_ola"]
irb(main):011:0>

robert
 

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
474,142
Messages
2,570,818
Members
47,362
Latest member
eitamoro

Latest Threads

Top