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
Related
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 want to convert "her" to "\"her".
I've tried using insert method:
>> "her".insert(0,'\"')
=> "\\"her"
and
>> "her".insert(0,'"')
=> ""her"
None of them gives me what I want: "\"her"
"her".insert(0,'"')
actually returns "\"her", which is what you said you wanted in the first place.
If you want to obtain "\"her\"", you might want to use Object#inspect:
"her".inspect
=> "\"her\""
Or, you can simply concatenate quotes at the beginning and at the end:
'"' + "her" + '"'
=> "\"her\""
If you just want "\"her"
a = "her".insert(0,'\"')
#=> "\\\"her"
puts a
#=> \"her
If you want output "\"her\""
a = "her"
#=> "her"
b = '\"'+a+'\"'
#=> "\\\"her\\\""
puts b
#=> \"her\"
I think your code is fine, you just don't know the meaning of \\ in console.
The first \ is escape character and second \ is character itself.
You will see \"her in the text foo.txt as you expected by:
File.write("foo.txt", "her".insert(0,'\"'))
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">
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.
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
examp = [["packed"], ["crud"], ["paced"], ["it"], ["emo"], ["wrote"], ["pcd"], ["ppcd"], ["pcd"]]
word = 'pcd'
foo = examp.select { |a| a[0][/[aeiou#{word}]/] }
p foo
Expected output:
[["paced"], ["pcd"]]
Actual output:
[["packed"], ["crud"], ["paced"], ["it"], ["emo"], ["wrote"], ["pcd"]]
Edit:
["ppcd"] and ["pcd"] (second time) added to the array. I forgot to mention that I also want to exclude words that have more than one occurrence of a letter in pcdaeiou, or words that appear more than once. Sorry.
Edit 2:
The specific problem is I want to filter an array by a given string (of letters) + some other letters. I don't want the out put to contain words with anything but, in the example, pcdaeiou. However, duplicates of aeiou are allowed, I just dont want repeated instances of p,c, or d.
[] in a regex denotes a character class. It matches a character that is any of the characters within it. So it's finding any word that contains a, e, i, o, u, p, c or d.
It's not clear what your really after though... Are you saying that a p, c, and d is required, but also allow vowels, but not any other consonants? If so, I'd say it's simplest to use two regexes. One to see if the letters you need are present, and another to make sure it only contains letters you allow.
And for all that is holy... use =~
examp.select do |a|
a[0] =~ /#{word}/ && a[0] =~ /^[#{word}aeiou]+$/
end
I assume the question is, "Select all arrays [str] from examp for which all characters of the string str are a, e, i, o or u, or one of the letters of the value of the variable word".
examp = [["packed"], ["crud"], ["paced"], ["it"], ["emo"], ["wrote"], ["pcd"]]
word = 'pcd'
p examp.flatten.select {|a| a.chars.all? {|c|
"aeiou#{word}".include?(c)}}.map {|e| [e]} # => [["paced"], ["pcd"]]
Edit: if, in my statement above of the question, each character of str "consumes" a matching character of "aeioupcd", then my solution could be modified as follows:
examp = [["packed"], ["crud"], ["paced"], ["it"], ["emo"], ["wrote"], ["pcd"], ["ppcd"]]
str = "aeiou#{word}" # => "aeioupcd"
p examp.flatten.select {|a| s = str.dup; a.chars.all? {|c|
s.delete!(c)}}.map {|e| [e]} # => [["paced"], ["pcd"]]
For a == "ppcd", "aeioupcd" is seen to contain the first "p" because "aeioupcd".delete!("p") returns "p" (rather than nil). However, delete! also removes this character from "aeioupcd", so the second "p" in "ppcd" is evaluated in "aeioucd".delete!("p") => nil, so "ppcd" is not selected.
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