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.
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 11 months ago.
The community is reviewing whether to reopen this question as of 10 months ago.
Improve this question
I have a number of numbered files, e.g.:
alpha_01.txt alpha_02.txt beta_01.txt beta_02.txt
I want to execute a single line bash that will output correctly merged files based on their variable name (e.g. alpha, beta, ...), that is, alpha.txt beta.txt.
I can do so for a single file:
cat alpha_*.txt(n) >>alpha.txt 2>/dev/null
But I don‘t know the name before _*.txt.
Can I use a wildcard here? Or what would be the best solution?
If you want to concatenate all the alpha_xxx.txt files then you cannot have beta_xxx.txt in the arguments of cat.
As #tripleee said, the easiest way would be to use a for loop where you list all the prefixes:
for name in alpha beta
do
cat "$name"_*.txt > "$name".txt
done
Now, if you don't know the prefixes in advance then you can always workout something with awk:
awk '
BEGIN {
for (i = 1; i <= ARGC; i++) {
filename = ARGV[i]
if (filename !~ /^(.*\/)?[^\/]+_[0-9]+\.[^\/.]+$/)
continue
match(filename, /^(.*\/)?[^\/]+_/)
prefix = substr(filename, RSTART, RLENGTH-1)
match(filename, /\.[^.\/]+$/)
suffix = substr(filename, RSTART, RLENGTH)
outfile[filename] = prefix suffix
}
}
FILENAME in outfile { print $0 > outfile[FILENAME] }
' ./*.txt
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"]
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
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 want to split the string from the last underscore. The string as below:
"abc_123_identifier_12345"
the output:
["abc_123_identifier", "12345"]
please tell me if you have same good ideas. Thanks in advance!
Try this:
"abc_123_identifier_12345".split(/_(\d+)$/)
#=> ["abc_123_identifier", "12345"]
a = "abc_123_identifier_12345"
a.rpartition('_') - ['_']
output in console
[22] pry > a = "abc_123_identifier_12345"
=> "abc_123_identifier_12345"
[23] pry > a.rpartition('_') - ['_']
=> ["abc_123_identifier", "12345"]
Looks more like a pattern matching task than a splitting task for me:
[1] pry(main)> /^(.*)_(\d*)$/.match("abc_123_identifier_12345").captures
=> ["abc_123_identifier", "12345"]
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
New to this forum.
Trying to run a test to find â or €
Using assertion
text.include? "â" , "€"
But getting errors.
Try:
text.include?("â") || text.include?("€")
OR:
/â|€/.match(text)
I would do it this way, since the include? method does not take an array:
['â', '€'].any? { |char| text.include?(char) }
I assume you're using minitest? You can use assert_match:
assert_match(/[â€]/, "text with â")
#=> true
assert_match(/[â€]/, "text with €")
#=> true
assert_match(/[â€]/, "text without")
#=> MiniTest::Assertion: Expected /[â€]/ to match "text without".