Get the sub string between two bracket in ruby on rails [closed] - ruby-on-rails

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have following strings:
1) Beach Cottage (CP)
2) Beach Cottage (AP)
3) Cotta (GAP)
And I want to get substing between ( ) that is from first CP

Try this for example:
str = "Beach Cottage (CP)"
str.match(/(\((.*)\))/)[2]

you can use scan also with regx:
str = "Beach Cottage (CP)"
needed_sub_str = str.scan(/\((.*)\)/)
puts "expected sub string :: #{needed_sub_str}"
Output ::
expected sub string :: CP

Related

How to matche all the words from a string that starts and ends with double underscore in rails [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
str = "Hello my name is __john__ and i am __30__ years old"
str.scan(/__(.*?)__/)
OUTPUT: [["john"], [30]]
Expected result: ["john", 30]
As #aspend mentioned above you will get result just by using .flatten property of array class:
<% str = "Hello my name is __john__ and i am __30__ years old"%>
<%=str.scan(/__(.*?)__/).flatten %>
Preview:

How to find total amount from a string using regex [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Hi i am working on a ruby on rails project wih ruby-2.5.0 and rails 5. I have a string which contains the total amount. So i need to find the total amount using regex.
String:-
"TOTAL
EFT
CHANGE
Taxable Ite.s
TOTAL includes GST
OTHER SAVINGS
0000000000
$73.26
HAIERF(RDS stm IGA
KARAWARA *AOI
TERMINAL"
Here $73.26 is the total amount. I tried /$(\d{1,2}(\.)\d{1,2})/ and /^\d{1,4}(\.\d{0,2})?$/ But its not working. Please help me with exact regex. Thanks in advance.
Updated As per the op's comment:
/TOTAL(?=((?!TOTAL).)*).*?(\$\d{1,4}(?:\.\d{1,2})?)/im
This is a compilation of both of your regex.
Regex Demo
Sample Source ( Run Here )
re = /TOTAL(?=((?!TOTAL).)*).*?(\$\d{1,4}(?:\.\d{1,2})?)/im
str = 'TOTAL $234
EFT
CHANGE
Taxable Ite.s
TOTAL includes GST
OTHER SAVINGS
0000000000
$73.26
HAIERF(RDS stm IGA
KARAWARA *AOI
TERMINAL
$83.26
Total
asdasfd
sadfasdf
$1235
'
str.scan(re) do |match|
puts match[1]
end

What is smartest way to parse string? (ios) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I got coming string from request like:
1 Ghanaian Cedi = 155.15541 Zimbabwe dollar
I want to have 155.15541 in double. How can I do what?
I hear about predicate, can it help here?
If you'd like to parse this string into components separated by the blank spaces use
NSArray *components = [string componentSeparatedBy:#" "];
to get an NSArray in which each index contains each group of characters. In this particular case, your string would return an array containing:
[#"1", #"Ghanaian", #"Cedi", #"=", #"155.15541", #"Zimbabwe", #"dollar"]
If the desired double is always at index 4, you can convert the corresponding string to a double using
double result = [[components objectAtIndex:4] doubleValue];
If the double isn't always at the same index, maybe you could use a regex to identify the double(s).

Programming with Erlang [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm learning Erlang and I'm trying to understand this code that was used as an example.
-module(tutorial5).
-export([format_temps/1]).
format_temps(List_of_cities) ->
convert_list_to_c(List_of_cities).
convert_list_to_c([{Name, {f, F}} | Rest]) ->
Converted_City = {Name, {c, (F -32)* 5 / 9}},
[Converted_City | convert_list_to_c(Rest)];
convert_list_to_c([City | Rest]) ->
[City | convert_list_to_c(Rest)];
convert_list_to_c([]) ->
[].
I am unsure of how to use these methods to get what I need. The most I know about this is that I'm supposed to be able to form a list of cities and their temperatures, and then be able to convert their temperatures from farenheit to celsius and vice versa. Any HELP would be appreciated.
The only callable function in the module tutorial5 is format_temps/1 (it takes one argument). It takes a list of city/temp where each city/temp is a tuple of the form {City,{f,Fahrenheit}} for example {berlin,{f,60}}. The function return a list of city/temp where the temp part is now {c,Celsius}. An example call from the shell with its return would be:
> tutorial5:format_temps([{berlin,{f,59}},{london,{f,50}},{stockholm,{f,50}}]).
[{berlin,{c,15.0}},{london,{c,10.0}},{stockholm,{c,10.0}}]
Some points to note are:
When calling a function in another module you MUST always include the module name
The words starting with lower case letters are atoms, literal constants with a name, while those starting with upper case letters (from my text) are variables. The look alike but are very different.

iOS split string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
ex1) a:3:{i:0;s:6:"Apple";i:1;s:4:"Google";}
ex2) a:2:{i:0;s:13:"Area";i:1;s:10:"Place";i:2;s:10:"Location";}
ex3) a:4:{i:0;s:6:"Cool";i:1;s:6:"Nice";i:2;s:6:"Awesome";i:3;s:6:"Wonderful";}
ex4) ...
ex5) ...
this is Answers about our Survey. i received string from server to iPhone. something like that.
a:%d is words count about answer.
i:%d is Index about words. it is increase.
s:%d is length about word.
i don't know what will be word inside double quotation marks. (random)
i understood componentsSeparatedByString.
how to get words inside double quotation marks?
[str stringByReplacingOccurrencesOfString:#"\"" withString:#""];
This will get rid of the quotes. Combine this with my comment and you've got it all figured out.
NSMutableArray *arrSplitString = [NSMutableArray arrayWithArray:[originalString componentsSeperatedByString:#";"]];
for(int i=0; i<[arrSplitString count]; ++i) {
arrSplitString[i] = [arrSplitString[i] stringByReplacingOccurrencesOfString:#"\"" withString:#""];
}
If the ; isn't the char you need to split on, you'll need to change that to the char you need to split on. If you need to split on multiple delimiters, then you'll want to look at the componentsSeparatedByCharactersInSet method.

Resources