在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"]