RAJ
RAJ

Reputation: 898

Regular expression to fetch the value within all the " "

a='fdkfjsdflksdj lfkjdflksdjf["fdkljfdfl"]fkjdfldjkf["fdfdf"]dfdfsdfsdfsdfddfdfkdfj["fdfds"]fdfasdfds'

I need to fetch the values which is inside "" that means out put should be

fdkljfdfl
fdfdf
fdfds

I have written the below coding

puts a[/\["(.*)"\]/m]

but it returns

["fdkljfdfl"]fkjdfldjkf["fdfdf"]dfdfsdfsdfsdfddfdfkdfj["fdfds"]

Can you help me to take that particular string within ""

Upvotes: 0

Views: 55

Answers (2)

Gagan Gami
Gagan Gami

Reputation: 10251

Try:

a.scan(/"([^"]*)"/).flatten
#=> ["fdkljfdfl", "fdfdf", "fdfds"]

it will return the array of string

Upvotes: 1

vks
vks

Reputation: 67968

puts a.scan(/\["(.*?)"\]/m)

              ^^

Make your regex non greedy.Or use negation based regex.

puts a.scan(/\["([^"]*)"\]/m)

Upvotes: 2

Related Questions