Regular expression inquiry [closed] - ruby

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm using Ruby to do a simple substring type match. I'm trying to understand why the following happens:
irb(main):024:0> puts "match" if "foo" =~ /foo/
match
=> nil
irb(main):025:0> puts "match" if "foo" =~ /foo,/
=> nil
how can this regex be modified so that if any part of the search criteria matches "foo", a match is made?

You've got your comparisons backwards:
"foo".match(/foo,/) # See if "foo" matches the pattern "foo,"
# => nil
"foo,".match(/foo/) # See if "foo," matches the pattern "foo"
# => #<MatchData "foo">
The =~ operator is a bit of history that has fallen out of style because it's not self-explanatory.

You can use the scan method of String and pass in the regex you want to check against.
1.9.3p194 :008 > puts "match" if "foo".scan(/foo,/)
match
=> nil

Related

Regex to get any uppercase letter within a slug without query parameters [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
There are quite few solutions how to get query parameters, however I would like grab url without query parameters only if that matches any uppercase letter
So here is my REGEX to grab uppercase letters, however I don't have any to grab URL without parameters,
([B-Z]|A[^_]|A$)
Example:
localhost:8080/my-UrL/url?auth-token=AdfsfasdfsdjfkladjaDSfl # should match => "my-UrL/url"
localhost:8080/my-url/url?auth-token=AdfsfasdfsdjfkladjaDSfl # should match => nil`
localhost:8080/my-url/URL?auth-token=AdfsfasdfsdjfkladjaDSfl # should match => "my-url/URL"
\\/([\\w\\/-]*[A-Z][\\w\\/-]*)(?:\\?|$)
This matches your examples:
irb(main):012:0> r = Regexp.new "\\/([\\w\\/-]*[A-Z][\\w\\/-]*)(?:\\?|$)"
irb(main):013:0> r.match "localhost:8080/mY-url/url?asdasd"
=> #<MatchData "/mY-url/url?" 1:"mY-url/url">
The required regex is:
/(?<=0\/).*[A-Z]+.*(?=\?)/
Explaining the regex:
(?<=0\/) is a negative lookahead assertion which ensures that "0/" is matched but not included in the matched characters
(?=\?) is a positive lookahead assertion which ensures that "?" is matched but not included in the matched characters
.*[A-Z]+.* ensures that string matched should include one or more uppercase characters
"localhost:8080/my-UrL/url?auth-token=AdfsfasdfsdjfkladjaDSfl".match /(?<=0\/).*[A-Z]+.*(?=\?)/
=> #<MatchData "my-UrL/url">
"localhost:8080/my-url/url?auth-token=AdfsfasdfsdjfkladjaDSfl".match /(?<=0\/).*[A-Z]+.*(?=\?)/
=> nil
"localhost:8080/my-url/URL?auth-token=AdfsfasdfsdjfkladjaDSfl".match /(?<=0\/).*[A-Z]+.*(?=\?)/
=> #<MatchData "my-url/URL">

Ruby: Check if a string contains multiple values [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to see if any arbitrary characters c1,c2,c3... turn up in a given string.
I'm looking for a nice way to rework the following example:
val = String.new("abc123")
if val.include? "a" or val.include? "2" or val.include? "3"
...
end
Try something like this
arr = [ 'a', '2', '3' ]
s = "abc123"
arr.any? { |letter| s.include? letter } # Will return true if any element is present
I would use a regular expression.
string_to_check = "hello random abc123"
string_to_check =~ /[a23]/
That would return the position where the first match is. Then if you would want to know if it is true or false you could just do == 0 or != 0
(string_to_check =~ /[a23]/) != 0
Write as below :
val = "abc123"
val[/[1af]/] # => "a"
val[/[w6f]/] # => nil
So change your code as
if val[/[a23]/]
#...
end
See the documentation of str[regexp] → new_str or nil.
If a Regexp is supplied, the matching portion of the string is returned. If a capture follows the regular expression, which may be a capture group index or name, follows the regular expression that component of the MatchData is returned instead.

Matching lines w/o the occurence of a keyword with regex [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Here are the lines I want to match with Regex in Ruby:
Klima
kjhiasug-Klimaanlage
and here is the one I dont want to match:
keine Klima
For that I need a Regex that does exactly that. I have tried with
(?!keine)\s?Klima
but it doesn't seem to work.
Any suggestions on how to fix that?
r = /(?<!keine )Klima/
r =~ "Klima" # => match
r =~ "kjhiasug-Klimaanlage" # => match
r =~ "keine Klima" # => no match
I'd use something like:
foo = ["Klima", "kjhiasug-Klimaanlage", "keine Klima"]
foo.select{ |s|
s[/^Klima|\bKlimaan\B/]
}
This is returned when run in IRB:
=> ["Klima", "kjhiasug-Klimaanlage"]
\b and \B are word-break markers, used to mark where a word transitions from "word" characters to non-word characters. \b marks the word-break boundary, and \B is the opposite, i.e., where a word DOESN'T break. The characters -K have a word-break between them, so \b matches at that point. nl has no break because both letters are word-characters in the character-set making up \w ([a-zA-Z0-9_]), so \B matches.
So, basically /^Klima|\bKlimaan\B/ says, "find strings starting with Klima, or where Klimaan begins a word but doesn't end it.
Can you use the inverse matching operator !~
See this question about it:
Does Ruby regular expression have a not match operator like "!~" in Perl?
irb(main):001:0> 'keine Klima' !~ /keine Klima/
=> false
irb(main):002:0> 'kjhiasug-Klimaanlage' !~ /keine Klima/
=> true
irb(main):002:0> 'Klima' !~ /keine Klima/
=> true

Check whether string contains a full word [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Is there something in Ruby that returns true only if the string contains the whole word, in this case 'hello'?
I don't want the behavior of include? which returns true if only some characters within a word are present:
'hello whats up'.include? 'll'
=> true
> 'hownowbrowncow'['now'] => "now" # will be nil if not found
And if you want it to require word boundaries...
'hownowbrowncow'[/\bnow\b/] => nil
It doesn't really matter if it returns true or false, since anything other than nil or false will fulfill a conditional test. If you really like true and false, well, you can turn an arbitrary expression into true or false a number of ways, one way is:
'now'.nil? => false
!'now'.nil? => true
Lots of good ways to do this as others have given. Here's another way:
my_string.scan(/\w+/).include? "hello"
This would be a case-sensitive check. For case insensitive:
my_string.scan(/\w+/).map(&:downcase).include? "hello"
This will also work with a variable for "hello":
m_string.scan(/\w+/).map(&:downcase).include? my_word
str = "Hello guys, how are you?'"
keyword = "hello"
print str.split.map { |word| word.downcase }.include? keyword.downcase
If you mean only the alphabet, then you could
(! 'Wr4ngle'.gsub(/[A-Za-z]/, '').blank?)
That grep should be versatile enough for your needs
It sounds like you're asking to simply do:
"hello" == "hello"
If you don't want the match to be case sensitive, you could use:
"HeLlO".downcase == "hElLo".downcase
If you are looking to see if a string of characters includes another string of characters, you could do something like this:
s1 = "hello"
s2 = "oelhl"
s1.split("").sort == s2.split("").sort #=> case sensitive
s1.downcase.split("").sort == s2.downcase.split("").sort #=> not case sensitive

Regular expression for series of numbers in ruby [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to match a pattern as given below
pattern: file.update.20120304.xml
file.update.1.xml
file.update.201203040002.xml
If i have to match the pattern leaving the numbers file.update..xml
how can I do in ruby?
"file.update.20120304.xml"[/\d+/] # => "20120304"
"file.update.1.xml"[/\d+/] # => "1"
"file.update.201203040002.xml"[/\d+/] # => "201203040002"
You can use Regexp and gsub to extract the numbers
filename = 'file.update.20120304.xml'
numbers = filename.gsub(/\Afile\.update\.(\d+)\.xml\z/, '\1')
The used regexp is composed as following:
a first part file\.update\. to be sure you have "file.update"
a middle part ([0-9]+) to have one or more number
an end part \.xml to be sure to have ".xml" at the end
the anchors \A and \z for the beginning and the end of the string
The middle part is wrapped with () to be a Regexp variable that you can reuse in the replacement string as \1
Update with variableName[/regular expression/]
filename = 'file.update.20120304.xml'
numbers = filename[/\Afile\.update\.(\d+)\.xml\z/, 1]
It returns the first captured group, i.e. between ().
p "file.update.201203040002.xml".split(".") .map { |x| x if x.to_i != 0}.compact[0].to_i
#=> 201203040002
p "file.update.20120304.xml".split(".") .map { |x| x if x.to_i != 0}.compact[0].to_i
#=> 20120304

Resources