ruby regular expression match string between last two delimiters [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 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

Related

Fetch/extract the .extension from a file path [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 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"]

Ruby - Split a String to retrieve a number and a measurement/weight and then convert numberFo [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 need to split a string, for food products, such as "Chocolate Biscuits 200g"
I need to extract the "200g" from the String and then split this by number and then by the measurement/weight.
So I need the "200" and "g" separately.
I have written a Ruby regex to find the "200g" in the String (sometimes there may be space between the number and measurement so I have included an optional whitespace between them):
([0-9]*[?:\s ]?[a-zA-Z]+)
And I think it works. But now that I have the result ("200g") that it matched from the entire String, I need to split this by number and measurement.
I wrote two regexes to split these:
([0-9]+)
to split by number and
([a-zA-Z]+)
to split by letters.
But the .split method is not working with these.
I get the following error:
undefined method 'split' for #MatchData "200"
Of course I will need to convert the 200 to a number instead of a String.
Any help is greatly appreciated,
Thank you!
UPDATE:
I have tested the 3 regexes on http://www.rubular.com/.
My issue seems to be around splitting up the result from the first regex into number and measurement.
One way among many is to use String#scan with a regex. See the last sentence of the doc concerning the treatment of capture groups.
str = "Chocolate Biscuits 200g"
r = /
(\d+) # match one or more digits in capture group 1
([[:alpha:]]+) # match one or more alphabetic characters in capture group 2
/x # free-spacing regex definition mode
number, weight = str.scan(r).flatten
#=> ["200", "g"]
number = number.to_i
#=> 200
I'm not an expert in ruby, but I guess that the following code does the deal
myString = String("Chocolate Biscuits 200g");
weight = 0;
unit = String('');
stringArray = myString.split(/(?:([a-zA-Z]+)|([0-9]+))/);
stringArray.each{
|val|
if val =~ /\A[0-9]+\Z/
weight = val.to_i;
elsif weight > 0 and val.length > 0
unit = val;
end
}
p weight;
p unit;

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

Why does my ruby regexp match never stop? [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 have a trouble with a regexp I wrote in ruby:
reg = /\([^\(|\)]{5,}*\)/i #almost 5 caracters inside two parenthesis.
one_string = "( foobarbaz, foobarbaz "
one_string.match(reg)#works fine and return nil
one_string = "( foobarbaz, foobarbaz, foobarbaz, foobarbaz foobarbaz, foobarbaz, foobarbaz, foobarbaz foobarbaz "
one_string.match(reg) # never stop if one_string is to long.
The parenthesis is not closed in one_string. And if the string I want to match is long, the match function does not seem to stop. Should I write my regexp differently, or is there a trouble with ruby (the expression is simple)?
Your regular expression syntax is incorrect here.
\( # match '('
[^\(|\)]{5,} # match any character except: '\(', '|', '\)' (at least 5 times)
Then it fails on the * quantifier because the preceding token is not quantifiable. Also you can drop the i flag since you are not matching any word characters in your regular expression.
I am not clear on what you are exactly trying to do here, but you may be looking for something like this.
reg = /\([^()]{5,}\)?/
Which I still don't understand the concept, if you are just trying to match everything between:
reg = /\([^()]*\)?/
Explanation:
\( # match '('
[^()]* # any character except: '(', ')' (0 or more times)
\)? # ')' (optional)

Retrieving multiple matched tokens from a regexp [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
Here's what I want to happen
> /x(y\d)*/.somefunction('xy1y2y3').each { |x| puts x }
y1
y2
y3
This seems like a pretty natural use of the asterisk in a regexp. I've matched a bunch of tokens and I want them printed out.
The closest I've been able to find is:
/x((y\d)*)/.match('xy1y2y3')[1].scan(/y\d/).each { |x| puts x }
Which is just abysmal.
The issue you are running into has to do with the regex rather than Ruby. You are repeating a capture group rather than capturing a repeated group. You could use
str.scan(/x((?:y\d)*)/)
However, this will capture all of the groups combined as one string. In order to do what you actually want to do (check that the string follows the pattern x followed by these groups) you unfortunately need to do two steps as you are doing in your question. Either that, or you can remove the additional requirement and search only for the pattern as other answers are suggesting.
I assume this is what you want:
'xy1y2y3'.gsub(/y\d/) { |s| puts s }
The gsub method accepts a block.
Based on your input and output, this looks about right:
'xy1y2y3'.scan(/y\d/)
# => ["y1", "y2", "y3"]
Use this if you want to print them:
puts 'xy1y2y3'.scan(/y\d/)
# >> y1
# >> y2
# >> y3
String's scan is your friend if you want to look through a string and capture repeating patterns.

Resources