Fetch/extract the .extension from a file path [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 4 years ago.
Improve this question
I need to extract the . extension from the following paths:
(_OasvkDSOEee_ruCXphIMsQ) /com.c.rb.gold.gbl.fw.proxy.component_jar/src/main/java/com/roup/ebus/mobile/api/common/resource/APIProxyResource.java
(_7ZgAUO-qEeeFqO9kl3sUYw) /cbmo-thgcb-ext-gm.war/src/main/app/WEB-INF/classes/rules/THMBK/APIRequestResponseMapper.xml
(_TM6vEFKjEee-NMziq4x8wA) /com.citi.rb.gold.memfis.sb.war/src/main/webapp/citibank/eclipselite/bank/memfis/sb/maintenance/SBBondCalculatorPopup.jsp
Every filepath above starts with "n" number of white-spaces. I need only the . and the part that follows it, such as:
.java
.xml
.jsp
.anything
Please help on it.

You can use File.extname:
File.extname("/abcd/INF/classes/rules/THMBK/APIRequestResponseMapper.xml")
=> .xml
File.extname("fdsfdsdf /abcd/INF/classes/rules/THMBK/APIRequestResponseMapper.xml")
=> ".xml"

If you have the strings in an array like so:
strings = [
"(_OasvkDSOEee_ruCXphIMsQ) /com.c.rb.gold.gbl.fw.proxy.component_jar/src/main/java/com/roup/ebus/mobile/api/common/resource/APIProxyResource.java",
"(_7ZgAUO-qEeeFqO9kl3sUYw) /cbmo-thgcb-ext-gm.war/src/main/app/WEB-INF/classes/rules/THMBK/APIRequestResponseMapper.xml",
"(_TM6vEFKjEee-NMziq4x8wA) /com.citi.rb.gold.memfis.sb.war/src/main/webapp/citibank/eclipselite/bank/memfis/sb/maintenance/SBBondCalculatorPopup.jsp"
]
You can get the extensions like so:
strings.flat_map do |string|
# in single line, regex is /(\.[^\.]+)$/
# here is multiline form with explanation:
regex = %r{
( # start of match group
\. # period
[^\.]+ . # any number of chars other than period
) # end of match group
$ # end of string
}x
string.match(regex).captures
end
# => [".java", ".xml", ".jsp"]

Related

regex to get the most occurred value on a string (you don't know the value) in ruby [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a list of IPs in a text document, I use File.open or File.readline to keep inside a string and I have to build a Regex to get the most occurred IP in the list.
This is what I have so far:
file = File.open("/Users/leonardoeiki/workspace/foo.txt", "r")
ips = Array.new
file.each_line do |line|
array = line.match("some regex that return the ips")
end
# some code that return me the most occurred ip on the
# array file = File.read("/Users/leonardoeiki/workspace/foo.txt")
There are other values in the archive like hours, alerts and errors messages, but I only need to return the most occurred ip
Based on the following text file
#file.txt
some text 192.168.1.1 some more text
some text 108.302.22.108 some more text
some text 108.302.22.108 some more text
Your ruby code should work with this
ips = File.readlines("file.txt").map do |line|
line.scan(/\d{1,}\b/).join('.')
end
ips.map{|ip| {ip: ip, count: ips.select{|i| i == ip }.count}}.max_by{|h| h[:count]}
#=> {:ip=>"108.302.22.108", :count=>2}

Regex for price with and comma [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 6 years ago.
Improve this question
I have "8 560,90 cur.". How do I get the whole number 8560 as integer?
I can split it by comma, and then get [0] as whole number, asked if there's more way to do it.
Here's how I'd do it:
str = "8 560,90 cur."
str.gsub(/[^\d,]/, '').to_i
# => 8560
This removes every character that isn't a digit or a comma, yielding "8560,90", then calls to_i on it, which gives 8560. This will work for any string as long as you want every digit before the first comma to be part of the number, and none after.
"8 560,90 cur.".scan(/(\d*?).+?(\d+,\d\d)/).flatten.join.to_i
# => 8560
"sdwfdsf560,90 cur.".scan(/(\d*?).+?(\d+,\d\d)/).flatten.join.to_i
# => 560

ruby regex replace the corresponding string [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
str = "1627207:132069:color:green;20518:28421:size:62cm"
aliastr = "20518:28421:S;20518:28358:L;20518:28357:M;1627207:132069:red"
How to dynamic replace str to "1627207:132069:color:red;20518:28421:size:S".
It was a pretty unclear question, but I think I got it now. Your aliastr contains mappings which control the replacements, i.e., the key '20518:28421:' should map to value 'S' and the key '1627207:132069:' should map to 'red'. Then you want to search for those keys in str and replace their current value with that new value. This does that:
str = "1627207:132069:color:green;20518:28421:size:62cm"
aliastr = "20518:28421:S;20518:28358:L;20518:28357:M;1627207:132069:red"
mapping = Hash[aliastr.scan(/(\d+:\d+:)(.*?)(?:;|$)/)]
# mapping = {"20518:28421:"=>"S", "20518:28358:"=>"L", "20518:28357:"=>"M", "1627207:132069:"=>"red"}
replaced = str.gsub(/(\d+:\d+:)(\w+:).*?(;|$)/) do |match|
key = $1
value = mapping[$1]
key + $2 + value + $3
end
p replaced
# => "1627207:132069:color:red;20518:28421:size:S"
Your question is not very clear, and probably contains an error ("color:red" in your wanted result vs. "red" in aliastr).
You may try something like this:
str = "1627207:132069:color:green;20518:28421:size:62cm"
aliastr = "20518:28421:S;20518:28358:L;20518:28357:M;1627207:132069:red"
replacements = aliastr.split(";").map{|s| parts=s.split(":"); [/#{parts[0]}:#{parts[1]}:.*/,s]}
src = str.split(";")
src.map{|s| replacements.each{|r| s.sub!(r[0],r[1])}; s }.join(";")

Not able to find a particular regex 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 9 years ago.
Improve this question
This is my regex /(.+)(\.|::)(\S+)\Z/. User enter function.[] Output $1 as function and $3 as []user enter function[] output nil The desired output is $1 as function and $3 as [].
Any guesses how can I alter the above regex to do this.
Call the match method to set $1 and $3:
/(\w+)(\.|::)?(\S+)\Z/.match('mongo.[]')
$1 # => mongo
$3 # => []
/(\w+)(\.|::)?(\S+)\Z/.match('mongo[]')
$1 # => mongo
$3 # => []
Are you looking for /(.+)(\.|::)*(\S+)\Z/?
The asterisk that I added means zero or more.
Or /(.+)(\.|::)?(\S+)\Z/,
The question mark means zero or one.

ruby regular expression match string between last two delimiters [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 need to match everything between last two '/' in a regex
for example: for string tom/jack/sam/jill/ ---> I need to match jill
and in that case also need to match tom/jack/sam (without the last '/')
Thoughts appreciated!
1)
str = "tom/jack/sam/jill/"
*the_rest, last = str.split("/")
the_rest = the_rest.join("/")
puts last, the_rest
--output:--
jill
tom/jack/sam
2)
str = "tom/jack/sam/jill/"
md = str.match %r{
(.*) #Any character 0 or more times(greedy), captured in group 1
/ #followed by a forward slash
([^/]+) #followed by not a forward slash, one or more times, captured in group 2
}x #Ignore whitespace and comments in regex
puts md[2], md[1] if md
--output:--
jill
tom/jack/sam
If what you want is given a string tom/jack/sam/jill/ extract two groups: jill and tom/jack/sam/.
The regexp you need is: ^((?:[^\/]+\/)+)([^\/]+)\/$.
Note that regexp does not accept / in the begin of string and request a / in the end of string.
Take a look: http://rubular.com/r/mxBYtC31N2

Resources