How do you say not to match in Ruby Regex
ex. you do not want to return true if it sees 'error'
/\b(error)\b/i
I know this returns true when it sees error, how do you say 'not' in this case? thanks!
Use the proper Ruby operator:
/\b(error)\b/i !~ someText
I would do something like the following excuse the /error/ pattern not sure exactly what you want to match here
return true unless b =~ /error/
or
return true if b !~ /error/
Related
How can I check if a string include symbol? It looks confusing to me:
class Beba
def initialize
while true
puts "Qfar emri deshironi ti vnoni bebes?"
##emri = gets.chomp.capitalize
if ##emri.scan(/\d+/).empty? && ##emri.scan(/\/./).empty?
puts "Ti e emertove beben me emrin: #{##emri}"
break
else
puts "Emri nuk mund te jete me numra/simbole, provoni perseri."
end
end
end
end
As you can see, at if##emri.scan(/\d+/).empty? && ##emri.scan(/\/./).empty?, I don't know what to do, like which method can I use for ##emri.scan(/\\.\).empty? to check if my string doesn't include any symbol?
For the specific characters you asked for, you can use this:
##emri.scan(/[!##$%^&*()_+{}\[\]:;'"\/\\?><.,]/).empty?
Will return true if no special character is found.
str !~ /[!##$%^&*()_+{}\[\]:;'"\/\\?><.,]/
returns true if and only if the string str contains none of the characters in the regex's character class (else false is returned).
Its seems you are looking for special characters.
Use something like
"Hel#lo".index( /[^[:alnum:]]/ )
It will return nil if no special charatcters.
[:alnum:] includes all 0-9, a-z, A-Z.
IF YOU WANT TO GO FOR SPECIFIC CHARATCERS
place all characters in a string & create regex like
characters = "!##$%^&*()_+{}[]:;'\"\/?><.,"
regex = /[#{characters.gsub(/./){|char| "\\#{char}"}}]/
& than use this regex to see if any of them exist in string like
if some_string =~ regex
I'm trying to create a regex to match only index urls (with or without parameters) in rails.
The following three match what I expect:
regex = /^http:\/\/localhost:3000\/v2\/manufacturers\/?(\S+)?$/
regex.match?('http://localhost:3000/v2/manufacturers?enabled=true')
#=> true
regex.match?('http://localhost:3000/v2/manufacturers/')
#=> true
regex.match?('http://localhost:3000/v2/manufacturers')
#=> true
I expect the regex not to match these:
regex.match?('http://localhost:3000/v2/manufacturers/1')
#=> true
regex.match?('http://localhost:3000/v2/manufacturers/123')
#=> true
regex.match?('http://localhost:3000/v2/manufacturers/1?enabled=true')
#=> true
Edit:
I'm so sorry but I forgot to mention that it should match:
regex.match?('http://localhost:3000/v2/manufacturers/1/models')
as it is a valid index url
You may use
/\Ahttp:\/\/localhost:3000\/v2\/manufacturers(?:\/?(?:\?\S+)?|\/1\/models\/?)?\z/
See the Rubular demo
Pattern details
\A - start of string
http:\/\/localhost:3000\/v2\/manufacturers - a http://localhost:3000/v2/manufacturers string
(?:\/?(?:\?\S+)?|\/1\/models)? - an optional sequence of:
\/? - an optional / char
(?:\?\S+)? - an optional sequence of ? and 1+ non-whitespace
| - or
\/1\/models\/? - /1/models string and an optional / at the end
\z - end of string.
You could change the end of your regex from:
\/?(\S+)?$/
to:
\/?(?:\?\S+|\d+\/\S+)?$
That would create an optional noncapturing group (?:\?\S+|\d+\/\S+)?.
Match \?\S+ for your questionmark and non whitespace chars
or |
Match \d+\/\S+for the added case of 1/models
Demo
The ? character makes the character optional
This works for me: http:\/\/localhost:3000\/v2\/manufacturers?\/?
r = /http://localhost:3000/v2/manufacturers?/?$/
r.match('http://localhost:3000/v2/manufacturers/1?enabled=true')
=> nil
r.match('http://localhost:3000/v2/manufacturers/1')
=> nil
I want to perform an action if a string is contained, non-case-sensitively, in another string.
So my if statement would look something like this:
#a = "search"
if #a ILIKE "fullSearch"
#do stuff
end
You can use the include? method. So in this case:
#var = 'Search'
if var.include? 'ear'
#action
end
Remember include? method is case-sensitive. So if you use something like include? 'sea' it would return false. You may want to do a downcase before calling include?()
#var = 'Search'
if var.downcase.include? 'sea'
#action
end
Hope that helped.
There are many ways to get there. Here are three:
'Foo'.downcase.include?('f') # => true
'Foo'.downcase['f'] # => "f"
Those are documented in the String documentation which you need to become very familiar with if you're going to program in Ruby.
'Foo'[/f/i] # => "F"
This is a mix of String's [] slice shortcut and regular expressions. I'd recommend one of the first two because they're faster, but for thoroughness I added it because people like hitting things with the regex hammer. Regexp contains documentation for /f/i.
You'll notice that they return different things. Ruby considers anything other than false or nil as true, AKA "truthiness", so all three are returning a true value, and, as a result you could use them in conditional tests.
You can use a regexp with i option. i for insensitive I think.
a = "fullSearch"
a =~ /search/i
=> 4
a =~ /search/
=> nil
Or you could downcase your string and check if it's present in the other
a = "fullSearch"
a.downcase.include?('search')
=> true
So
shout = "gabba gabba hey"
of course does
shout.include?("gabba")
=> true
shout.include?("nothing")
=> false
Also this works
shout.include?("gabba"||"nothing")
=> true
However this doesn't
shout.include?("nothing"||"gabba")
=> false
I'm confused. Doesn't this operator work in an include at all, does it stop after evaluating the first value no matter if it returns true or false, or am I just missing something essential? Of course I could use this far longer code
shout.include?("nothing") or shout.include?("gabba")
=> true
but I'd rather have it short and concise.
You should use regexp instead:
!! (shout =~ /(gabba|nothing)/)
/(gabba|nothing)/ is a regular expression matching 'gabba' or 'nothing'
=~ returns the position of the regular expression in your string, if found
!! makes sure the result of the operation is true or false
Basically, you can't.
What you tried:
shout.include?('gabba' || 'nothing')
is equivalent to
shout.include?('gabba')
because
'gabba' || 'nothing'
# => 'gabba'
and this is how || operator works in Ruby. It returns first operand unless it's false or nil. Otherwise, it returns second operand. Since your first operand is 'gabba' string, it's being returned.
I'm trying to use regex as the conditional in a Ruby (1.9.2) if statement but it keeps returning true even when the regex evaluates to nil
if (params[:test] =~ /foo/)
return "match"
else
return "no match"
end
The above returns "match" even when Rails.logger.info(params[:test]) shows test as set to "bar"
if params[:test] =~ /foo/
# Successful match
else
# Match attempt failed
end
Works for me. Debug what is in params[:test]