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(";")
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 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 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}
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
I have list of items in a weblist and which has both parent and child. Child is indented to the right, I need to retrieve values of child and parent in two different columns in a datatable.
My code goes like this:
list = qtp_getroproperty(page.weblist(), "items count", itemsCount
For n = 1 To itemsCount
items = page.weblist(), getitem(n)
In VBScript it's Left():
>> For Each s In Array("x", " x", " x")
>> WScript.Echo s, CStr(" " = Left(s, 1))
>> Next
>>
x Falsch
x Wahr
x Wahr
>>
Try this
if strSurname.StartsWith(" ")
There are several ways to go about this:
Extract the first character with the Left function, as Ekkehard Horner suggested:
If Left(str, 1) = " " Then
...
End If
Check the first character with the InStrRev function:
If InStrRev(str, " ", 1) > 0 Then
...
End If
LTrim the string and compare it to the original string:
If LTrim(str) <> str Then
...
End If
Use a regular expression:
Set re = New RegExp
re.Pattern = "^ "
If re.Test(str) Then
...
End If
Note that this last approach is the most versatile, but also the most expensive. Usually it won't make sense to use this to check for something as simple as "does the string begin with a space". It becomes more useful if for instance you want to check "does the string begin with any kind of whitespace" ("^\s").
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 have an array with string values:
a = ["Customer name", "Address", "Qualification"]
Requirement is to make these string values enclosed in both single and double quotes like this:
a = ["'Customer name'", "'Address'", "'Qualification'"]
How can I achieve this?
a = ["Customer name", "Address", "Qualification"]
a.map { |i| "'#{i}'" } # => ["'Customer name'", "'Address'", "'Qualification'"]
It makes sense to say that you want to enclose the content of each string with single quotes, but it does not make sense to say that you want to have double quotes around it, that is part of the literal. But anyway,
a.map{|s| "'#{s}'"}
# => ["'Customer name'", "'Address'", "'Qualification'"]
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.