简体   繁体   English

如何从Rails中的ruby字符串中获取双引号字符

[英]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.

相关问题 在Ruby on Rails中双引号字符串中的#{$$}是什么? - What does #{$$} inside a double quoted string in Ruby on Rails ? Ruby on Rails:如何从字符串中删除/剪切前300个单词或字符? - Ruby on Rails: How can i take/cut first 300 words or characters from a string? 如何从url查询参数检查rails params hash包含双引号字符串? - How to check rails params hash from url query parameter contains double quoted string? Ruby on Rails:如何为请求标头创建带引号的字符串? - Ruby on Rails: How may create a quoted string to a request header? 如何检查Ruby on Rails控制器中是否有查询字符串? - How can I check if there is a query string from a Ruby on Rails controller? 如何从轨道上的字符串红宝石中删除双引号? - how Remove double quotes from a string ruby on rails? 在Rails中查询-查询中的双引号字符串 - Querying in rails - Double quoted string inside query 转义符&lt;%= h(“”)%&gt;以及如何在Ruby on Rails中处理双引号 - Escape characters <%= h(“”) %> and how to deal with double quote in Ruby on Rails 在 Rails 中,如何从 boolean 中获取字符串? - In Rails, how can I get a string from a boolean? ruby / rails ruby​​ / rails如何使用Twitter API从(用户的)Twitter LIST获取所有推文? - ruby/rails ruby/rails How can I get all the tweets from Twitter LIST (of users) using the Twitter API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM