[英]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
. 即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('”')]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.