How to combine these two Ruby string tests in one regular expression?
The most important improvement you can make is to also test that the word and the parentheseses have the correct relationship. If I understand correctly, "link(url link_name)"
should be a match but "(url link_name)link"
or "link stuff (url link_name)"
should not. So match "link"
, the parentheses, and their contents, and capture the contents, all at once:
"stuff link(url link_name) more stuff".match(/link\((\S+?) (\S+?)\)/)&.captures
=> ["url", "link_name"]
(&.
is Ruby 2.3; use Rails' .try :captures
in older versions.)
Side note: string.scan(regex).present?
is more concisely written as string =~ regex
.
Ruby regular expression using variable name
The code you think doesn't work, does:
var = "Value"
str = "a test Value"
p str.gsub( /#{var}/, 'foo' ) # => "a test foo"
Things get more interesting if var can contain regular expression meta-characters. If it does and you want those matacharacters to do what they usually do in a regular expression, then the same gsub will work:
var = "Value|a|test"
str = "a test Value"
str.gsub( /#{var}/, 'foo' ) # => "foo foo foo"
However, if your search string contains metacharacters and you do not want them interpreted as metacharacters, then use Regexp.escape like this:
var = "*This*"
str = "*This* is a string"
p str.gsub( /#{Regexp.escape(var)}/, 'foo' )
# => "foo is a string"
Or just give gsub a string instead of a regular expression. In MRI >= 1.8.7, gsub will treat a string replacement argument as a plain string, not a regular expression:
var = "*This*"
str = "*This* is a string"
p str.gsub(var, 'foo' ) # => "foo is a string"
(It used to be that a string replacement argument to gsub was automatically converted to a regular expression. I know it was that way in 1.6. I don't recall which version introduced the change).
As noted in other answers, you can use Regexp.new as an alternative to interpolation:
var = "*This*"
str = "*This* is a string"
p str.gsub(Regexp.new(Regexp.escape(var)), 'foo' )
# => "foo is a string"
Regular expressions in Ruby with special characters
if you already know you're always splitting on the same character, you can just provide the character as a string:
> "John Doe+123456".split('+') # no regular expression needed
=> ["John Doe", "123456"]
or if you have to use a regular expression, then escape the +
with a \
:
> "John Doe+123456".split(/\+/) # using a regular expression; escape the +
=> ["John Doe", "123456"]
last not least, here is another way to do this:
> "John Doe+123456".scan(/[^+]+/) # find all sequences of characters which are not a +
=> ["John Doe", "123456"]
Can I have an Array of regular expressions in ruby?
Can I have an Array of regular expressions in ruby?
Yes.
If there is a more sophisticated way [to test that a given string matches all regex]?
Use Enumerable#all?
.
patterns = [/foo/, /bar/]
input = 'baz'
patterns.all? { |pattern| pattern.match?(input) }
match?
was added recently, in ruby 2.4.
inserting variable value in regex in ruby script
First of all, regxPlayerVariable
is not a Regexp
, it's a String
. And the reason why your interpolation does not work is because you are using single quotes. Look:
foo = "bar"
puts '#{foo}' # => #{foo}
puts "#{foo}" # => bar
puts %q{#{foo}} # => #{foo}
puts %Q{#{foo}} # => bar
puts %{#{foo}} # => bar
puts /#{foo}/ # => (?-mix:bar)
puts %r{#{foo}} # => (?-mix:bar)
Only the last two are actually regular expressions, but here you can see which quoting expressions do interpolation, and which don't.
Related Topics
In Rails - Is There a Rails Method to Convert Newlines to <Br>
Problems with the Rails Console, Rvm and Readline
Ruby Hash Default Value Behavior
Rails with Ruby-Debugger Throw 'Symbol Not Found: _Ruby_Current_Thread (Loaderror)'
How to Encode Media in Base64 Given Url in Ruby
Best Way to Debug Third-Party Gems in Ruby
Warning: Constant ::Fixnum Is Deprecated When Generating New Model
How to Set Default Ruby Version with Rvm
How to Generate a Human Readable Time Range Using Ruby on Rails
Ruby Read CSV File as Utf-8 And/Or Convert Ascii-8Bit Encoding to Utf-8
Best Way to Highlight Current Page in Rails 3? - Apply a CSS Class to Links Conditionally
Finding Out Current Index in Each Loop (Ruby)
How to Compare Strings Ignoring the Case
Given an Array of Arguments, How to Send Those Arguments to a Particular Function in Ruby