Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如果我在文件中有一个字符串:
str = hi "Sonal"
我能够在字符串中获取这行文件。现在我想获取双引号之间的字符。即Sonal。我怎样才能在红宝石中做到这一点?
Sonal
尝试以下
'hi "Sonai"'.match(/"(?<inside_quote>.+)"/)[:inside_quote]
你可以像这样使用正则表达式,
given_string[/\".*\"/]
这将匹配引号下的字符。
或不使用正则表达式尝试类似 s[s.index('"')..s.rindex('"')]