Ruby remove substring matching to any element of array
2014-05-17 14:35
417 查看
Ruby remove substring matching to any element of array
I have a string saystr ="this is the string "and i have an array of string say
array =["this is" ,"second element", "third element"]I want to process the string such that sub string matching any of the element of array should be removed and rest of the string should be return.So i want the following output.
output: "the string "How can i do this in ruby.solution 1:
str = "this is the string " array = ["this is" ,"second element", "third element"] pattern = /\b(?:#{ Regexp.union(array).source })\b/ # => /\b(?:this\ is|second\ element|third\ element)\b/ str[pattern] # => "this is" str.gsub(pattern, '').squeeze(' ').strip # => "the string"
solution 2:
array =["this is" ,"second element", "third element"]str = "this is the string "str.gsub(Regexp.union(array),'') # => " the string "
相关文章推荐
- Matching Portions of A String to Elements of an Array in Ruby
- 步步为营VS 2008 + .NET 3.5(7) - LINQ查询操作符之First、FirstOrDefault、Last、LastOrDefault、ElementAt、ElementAtOrDefault、Contains、Any、All、Count、LongCount、Sum、Min、Max、Average、Aggregate、Cast、DefaultIfEmpty、SequenceEqual、OfType、ToArray、ToList、ToDictionary
- How to find number of a specific integer in an array of Fixnums ruby
- Converting a array to an array of ranges in ruby
- ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.al
- Ruby converting array of hashes to array of arrays
- Get nth element of an array in Ruby?
- Only push specific substring to array in Ruby
- Whats the best way to split an array in ruby into multiple smaller arrays of random size
- If you had an array of 1 million values and you had to remove duplicates
- Ruby: how to sort array of string parsing the content
- Convert array of key value object to object of the key values (ruby)
- how to merge array of hash based same keys in ruby?
- how to remove nil and blank string in an array in Ruby
- array summation and merging based on uniqueness of element at certain position + ruby arrays
- 7、步步为营VS 2008 + .NET 3.5(7) - LINQ查询操作符之First、FirstOrDefault、Last、LastOrDefault、ElementAt、ElementAtOrDefault、Contains、Any、All、Count、LongCount、Sum、Min、Max、Average、Aggregate、Cast、DefaultIfEmpty、SequenceEqual、OfType、ToArray、ToList、ToDictionary
- [jQuery] 如何根据名字来选取checkbox数组 -- How to get the array of checkbox via name
- Could not obtain connection to any of these urls: localhost:1099
- How to solve “add/remove operation is impossible, because the code element 'Cxxx' is read only”
- 【转】How to solve “add/remove operation is impossible, because the code element 'Cxxx' is read only”