Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在ruby中,如何将子字符串从一个模式转换为另一个模式?还有,我如何得到最后一个这样的模式?_Ruby_Regex_String_Grep_Substring - Fatal编程技术网

在ruby中,如何将子字符串从一个模式转换为另一个模式?还有,我如何得到最后一个这样的模式?

在ruby中,如何将子字符串从一个模式转换为另一个模式?还有,我如何得到最后一个这样的模式?,ruby,regex,string,grep,substring,Ruby,Regex,String,Grep,Substring,假设我有以下字符串: This is a test. This is a Test. The test_id = "miaomiao" 我只想把“妙妙”还给你 另外,只需补充:假设我有多个: "This is" a test. Thq3gis is a The test_id = "woofwoof"""""""""Test.qfqs This is "a testeqgq3. This" The test_id = "hisshiss"is a Test. This is a tfqef

假设我有以下字符串:

This is a test. This is a Test. The test_id = "miaomiao"
我只想把“妙妙”还给你

另外,只需补充:假设我有多个:

"This is" a test. Thq3gis is a The test_id = "woofwoof"""""""""Test.qfqs 
This is "a testeqgq3. This" The test_id = "hisshiss"is a Test. 
This is a tfqefest. This "is a Test". eqgThe test_id = "moomoo"
This is a test. This is gqwa Test. qefThe test_id = "roarroar"
This is a test. This is a Tqqest. faqThe test_id = "miaomiao"

我想再次返回“maiomiao”后一种情况下的整个内容是一个包含空格和返回的字符串。

您可以使用正则表达式提取双引号中的任何内容:

>> test_string = 'This is a test. This is a Test. The test_id = "miaomiao"'
=> "This is a test. This is a Test. The test_id = \"miaomiao\""
>> test_string.scan /".+"/
=> ["\"miaomiao\""]

或者,如果你只想知道引号里的内容,而不想知道引号里的内容

>> test_string[/"(.+)"/,1]
=> "miaomiao"
对于多个匹配,您可以使用

>> test_string
=> "This is a test. This is a Test. The test_id = \"miaomiao\" This is a test. This is a Test. The test_id = \"waowao\""
>> test_string.scan(/".+?"/)
=> ["\"miaomiao\"", "\"waowao\""]
对于结果中省略引号的多个匹配,请使用正向的前向/后向:

>> test_string.scan(/(?<=")(\w+?)(?=")/).flatten
=> ["miaomiao", "waowao"]
>测试字符串扫描(/(?[“喵喵”,“哇哇”]

使用此选项。尝试使用代码
str='这是一个测试。这是一个测试。测试\u id=“woofwoof”str[/=\s+”(\w+)“$/,1]#=>“woofwoof”
。你试过解决这个问题吗?你能发布你的代码吗?我对ruby很陌生。我的想法是将整个内容读入一个数组,然后得到一个子字符串。我正在寻找一种更快的方法。嗯,看起来你与数据输入人员有问题。可能想与HR聊天。或者这里的某人从未通过服务器进行过grepG
>> test_string.scan(/(?<=")(\w+?)(?=")/).flatten
=> ["miaomiao", "waowao"]