简体   繁体   中英

How can i get double quoted characters from a string in ruby on rails

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. ie Sonal . How can I do it in ruby?

尝试以下

'hi "Sonai"'.match(/"(?<inside_quote>.+)"/)[:inside_quote]

You can use regular expression like this,

given_string[/\".*\"/]

This will match the characters under quotes.

或不使用regexp尝试类似s [s.index('“').. s.rindex('”')]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM