Why does my ruby regexp match never stop? [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 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)

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"]

printf “%6.1f” 12.3456 printf”|%6s%8.2f|” hello 12.2456) explanation [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 5 years ago.
Improve this question
The following code returns the following values accordingly :
printf “%6.1f” 12.3456 printf”|%6s%8.2f|” hello 12.2456)
_ _ 12.3 |_hello_ _ _ 12.25|
The question is what does each character of the code mean and why does it return these values?
They seem homeworks and the code is... read this short tutorial which should help you solve it https://linuxconfig.org/bash-printf-syntax-basics-with-examples
As a hint: printf “%5.2f” 100.1555 Which means:
printf function that prints text to console
% print an argument
5.2f format of the argument
5 print integer part with 5 characters, if the integer part has less than 5 characters, fill it with spaces, if it has more it will NOT cut it
. separates the integer and decimal format syntax
2 print decimal part with 2 characters, if the integer part has less than 2 characters, fill it with spaces, if it has more cut it to meet 2 characters
f the argument is of type float. For more info https://www.le.ac.uk/users/rjm1/cotter/page_30.htm
100.1555 argument
The result would be: <space><space>100.15 (if you count the characters, there are 5 characters at the left of the dot, and 2 at the right --> 5.2)
For printf “%6.2f” 100.1555 result would be: <space><space><space>100.15
For printf “%6.3f” 100.1555 result would be: <space><space><space>100.155
For printf “%1.3f” 100.1555 result would be: 100.155 (integer part is never cut)

gsub a backslash to display a unicode character [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 7 years ago.
Improve this question
I would like to gsub one of the backslahes in front of u00E9 so that it will print the unicode character, which in this case would be e with an accent on top. Below is the code I am using, which doesn't work.
array1 = [
["V\\u00E9tiver (1978) ", "by L'Artisan Parfumeur", "12"],
["Time for Peace for Her (1999) ", "by Kenzo", "4"],
["Time for Peace for Him (1999) ", "by Kenzo", "7"],
[" Untitled (2009) ", "by Kenzo", "1"],
[" Havana Vanille (2009) ", "by L'Artisan Parfumeur", "10"]
]
array3 = array1.each do |s,a,r|
puts s.gsub(/\\/,"")
end
so what I would like to know is the correct regex to get rid of one of the backslashes in the array.I was thinking that the one I have above would be enough.However it is not.
You seem to not understand how escape sequences work. Take this string, for example:
s = "V\u00E9tiver (1978)"
The \u00e9 here is a representation of one character é, not a six-char string of \ u 0 0 e 9. So, if you try to replace any part of it (say, the "u"), you'll fail because there's no such character in the string.
s.gsub('u', 'U') # => "Vétiver (1978)"
Whereas in your string
s2 = "V\\u00E9tiver (1978) "
you have totally different situation. Here the backslash does not start a unicode escape sequence, but is instead escaped itself. Which means that the following characters u00E9 are just regular characters in the string, not part of unicode codepoint definition.
Off the top of my head, I don't know of a way to turn "\\u00E9" into "\u00E9" (short of eval, of course). What you should do instead, is fix the source of that data, so that it does not double-escape sequences.

Check if a string contains a sequence of "udlr" 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
If I have a string how can I check if the string contains any sequence of "rldu"? I am really new to ruby, sorry if this is a stupid question to ask.
r- right, l-left, d-down, u-up.
For example:
str = "udlv" #should return false
str = "lrd" #should return true
Assuming the string should entirely be composed of the given four characters in any order
str =~ /^[rldu]+$/
will return an integer or nil that you can use in a conditional. If you want a boolean, use the trick with !!:
!!str.match(/^[rldu]+$/)
If you wanted to check whether the string contains anything other than udlr, then
!("udlv" =~ /[^udlr]/) # => false
!("lrd" =~ /[^udlr]/) # => true
This one does not use a regular expression:
p "udlv".count("^rlrd").zero? #=> false
p "lrd".count("^rldu").zero? #=> true
"^rldu" means "everything else than rldu"
Assuming that by 'any sequence of "rldu"' you mean you want to verify that the string is composed of only the r, l, d, u (any number of times, in any order) and nothing else, a good old regular expression should work just fine:
str =~ /^[udlr]*$/
If you strictly need that to be a boolean value (true/false), then you can prefix it with two exclamation points (double not), like so:
!!(str =~ /^[udlr]*$/)
In most cases, you shouldn't need to do that because Ruby can interpret any value as either true or false anyway.
You can view the documentation for all of String's core methods here. And here is a guide on regular expressions.

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