Reputation: 1442
If I have a string in a file:
str = hi "Sonal"
I am able to fetch this line of file in a string. Now I want to fetch the characters between the double quotes. i.e. Sonal
. How can I do it in ruby?
Upvotes: 2
Views: 160
Reputation: 8065
You can use regular expression like this,
given_string[/\".*\"/]
This will match the characters under quotes.
Upvotes: 1
Reputation: 186
or without regexp try something like this s[s.index('"')..s.rindex('"')]
Upvotes: 0
Reputation: 29599
try the following
'hi "Sonai"'.match(/"(?<inside_quote>.+)"/)[:inside_quote]
Upvotes: 4