Ruby - remove pattern from string
Assuming that the "WBA" spot will be a sequence of any letter or number, followed by a space, dash, and space:
str = "WBA - Skinny Joe vs. Hefty Hal"
str.sub /^\w+\s-\s/, ''
By the way — RegexPal is a great tool for testing regular expressions like these.
Remove substring from the string
You can use the slice method:
a = "foobar"
a.slice! "foo"
=> "foo"
a
=> "bar"
there is a non '!' version as well. More info can be seen in the documentation about other versions as well:
http://www.ruby-doc.org/core/classes/String.html#method-i-slice-21
Removing a pattern from the beginning and end of a string in ruby
gsub can take a regular expression:
text.gsub!(/(<br \/>\s*)*$/, '')
text.gsub!(/^(\s*<br \/>)*/, '')
text.strip!
Is there a Regex Delete in Ruby?
One way is to add your own short methods:
class String
def del(regexp)
gsub(regexp,'')
end
def del!(regexp)
gsub!(regexp,'')
end
end
Typically this code would go in a lib/ directory, for example lib/string-extensions.rb
Heads up that some programmers really dislike this because it's monkey-patching. I personally like it for projects because it makes code easier to understand - once I have the "del" method, I can quickly see that calls to it are just deleting the regexp.
Remove a string pattern and symbols from string
arr = ["not12345", " notabc", "notone, nottwo", "notCAPSLOCK",
"##doublehash:", "h#a#s#h", "#notswaggerest"].
arr.flat_map { |str| str.downcase.split(',').map { |s| s.gsub(/#|not|\s+/,"") } }
#=> ["12345", "abc", "one", "two", "capslock", "doublehash:", "hash", "swaggerest"]
When the block variable str
is set to "notone, nottwo"
,
s = str.downcase
#=> "notone, nottwo"
a = s.split(',')
#=> ["notone", " nottwo"]
b = a.map { |s| s.gsub(/#|not|\s+/,"") }
#=> ["one", "two"]
Because I used Enumerable#flat_map, "one"
and "two"
are added to the array being returned. When str #=> "notCAPSLOCK"
,
s = str.downcase
#=> "notcapslock"
a = s.split(',')
#=> ["notcapslock"]
b = a.map { |s| s.gsub(/#|not|\s+/,"") }
#=> ["capslock"]
Ruby regex to remove / from string
You have to escape the forward slash because it's a special character. Something like this:
s = "This is a line / string"
s.gsub(/[\s\/]/, '') # => "Thisisalinestring"
Ruby- How to remove all words which have a specific pattern in a string
Similar to @Sam's answer, only smaller :) Uses the little known Enumerable#grep_v.
Inverted version of #grep. Returns an array of every element in enum for which not Pattern === element.
"I am very happy today".split.grep_v(/a/).join(' ') # => "I very"
Simple method to remove pattern matching regex from string in Ruby
No, that is the simplest you can do.
Related Topics
How to Write a Rails Mixin That Spans Across Model, Controller, and View
Perform One Validation Only If All Other Validations Pass
In a Sinatra App on Heroku, Session Is Not Shared Across Dynos
Create Hash from Array and Frequency
Built in Way to List Directories in a Directory in Ruby
Rvm: How to Use Gems from a Different Ruby
Warning: String Literal in Condition
Activerecord Association Select Counts for Included Records
Running Webrick Server in Background
Stream and Unzip Large CSV File with Ruby
Elasticsearch Terms Aggregation by Entire Field
How to Resolve Deprecation Warnings for Openssl::Cipher::Cipher#Encrypt