Rails, select last n characters from a string? [duplicate] - ruby-on-rails

This question already has answers here:
Extracting the last n characters from a ruby string
(9 answers)
Closed 7 years ago.
I have a string "foo bar man chu" and I want to select the last 5 characters from it.
The regex expression /.{5}$/ does the job of selecting them, but how do I save them to a string in Rails? gsub(/.{5}$/,'') removes them, kind of the opposite of what I want. Thanks!

The match method will return the result of attempting to match the string with the regular expression
result = "foo bar man chu".match(/.{5}$/)
puts result
=> "n chu"
If the regular expression is not matched, then nil will be returned.

Related

Regex for combination of at least one alphanumeric and special character [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I need to create a regex which contains at least 1 special character, and at least 1 number with alphabets.
You may try the following pattern:
^(?=.*[0-9])(?=.*[^A-Za-z0-9])(?=.*[A-Za-z]).*$
Explanation:
(?=.*[0-9]) assert one number present
(?=.*[^A-Za-z0-9]) assert one special character present
(?=.*[A-Za-z]) assert one alpha present
Note that I have defined a special character as being anything not alphanumeric. If instead you have a list of special characters, then you can modify the middle lookahead in my pattern.

Regexp does not match utf8 characters in words (\w+) [duplicate]

This question already has answers here:
How to match unicode words with ruby 1.9?
(3 answers)
Closed 9 years ago.
Why does the following code return nil:
'The name of the city is: Ørbæk'.match(/:\s\w+/)
#=> nil
When I would expect it to return "Ørbæk"
I have tried setting the #encoding=utf-8 in the beginning of the document but it does not change anything.
PS. Ø and Æ are danish letters
The metacharacters \w is equivalent to the character class [a-zA-Z0-9_]; matches only alphabets, digits, and _.
Instead use the character property \p{Word}:
'The name of the city is: Ørbæk'.match(/:\s\p{Word}+/)
# => #<MatchData ": Ørbæk">
According to Character Properties from Ruby Regexp documentation:
/\p{Word}/ - A member of one of the following Unicode general category Letter, Mark, Number, Connector_Punctuation
You can use \p{Word} instead:
irb(main):001:0> 'The name of the city is: Ørbæk'.match(/:\s\p{Word}+/)
=> #<MatchData ": Ørbæk">
If the word you want to match contains just letter characters, then use \p{L} :
match(/:\s\p{L}+/)

Erlang Tuple to String [duplicate]

This question already has answers here:
Convert erlang terms to string, or decode erlang binary
(2 answers)
Closed 9 years ago.
Is there a way how to convert a tuple to a string ?
Consider I have the following list :
[{atom,5,program},{atom,5,receiving},{nil,5}]
I wish to convert this into the following string:
"{atom,5,program},{atom,5,receiving},{nil,5}"
I have tried using erlang:tuple_to_list on each element in the list, which returns
A = [atom,5,program]
Eventually, I can't concatenate that with "{" ++ A ++ "}"
Any ideas how I can turn that to a string ?
Term = [{atom,5,program},{atom,5,receiving},{nil,5}].
lists:flatten(io_lib:format("~p", [Term])).

Rails split by last delimiter [duplicate]

This question already has answers here:
Ruby: Split string at character, counting from the right side
(6 answers)
Closed 9 years ago.
I want to be able to split strings into 2 elements, because each string would contain at least one delimiter.
Example: "hello_world". If I apply .split("_") then I receive: ["hello", "world"].
The problem arises when I have a string with two or more delimiters. Example "hello_to_you".
I want to receive: ["hello_to", "you"].
I know about the limit option for the split function: .split("_", 2), but it produces: ["hello", "to_you"].
So, basically, I would need to split the entire string ONLY with the last delimiter ("_").
This is exactly what String#rpartition does:
first_part, _, last_part = 'hello_to_you'.rpartition('_')
first_part # => 'hello_to'
last_part # => 'you'
try
'hello_to_you'.split /\_(?=[^_]*$)/
class String
def split_by_last_occurrance(char=" ")
loc = self.rindex(char)
loc != nil ? [self[0...loc], self[loc+1..-1]] : [self]
end
end
"test by last_occurrance".split_by_last #=> ["test by", "last"]
"test".split_by_last_occurrance #=> ["test"]

Array to single string [duplicate]

This question already has answers here:
Array.join("\n") not the way to join with a newline?
(7 answers)
Closed 5 years ago.
I'm having an issue which I can't seem to solve. I have an array which I need to convert to an single string. The elements need to be put underneath each other.
sample_array = ['a','b','c','d','e']
desired output:
sample_array = "a
b
c
d
e"
I thought I could do this with a 'heredoc', but I can only get the elements behind each other inline. This is unfortunately not what I need. Anyone who can help me?
edit for edit question
In a single line, you can use inject:
sample_array = ['a','b','c','d','e']
puts sample_array.inject(""){|conc,x| conc + "\n" + x }
=> "a b c d e"
that will fold the array recursively and adding a line between chars

Resources