Match last group within _()_ with regex in Ruby - ruby

I need to extract the last occurrence of a substring enclosed in _()_, e.g.
'a_long_(abc)_000' => abc
'a_long(string)_(def)_000' => def
'a_long_(string)_(abc)_blabla' => abc

Match using /_\((.*?)\)_/ and grab the last match:
>> 'a_long_(string)_foo_(abc)_blabla'.scan(/_\((.*?)\)_/)[-1]
=> ["abc"]

Smth like this:
str[/.*_\((.*?)\)_/,1]

You can also use the regex:
.*_\((.*?)\)_
See it

This this:
\(([^\)]+)\)_[a-zA-Z0-9]*$
$1 should be your string

Related

Ruby 2.3.4 -- How to delete a substring of a string?

For example: there has a string:
'Trump is great#Trump is great#'
If I do:
'Trump is great#Trump is great#'.delete! 'Trump is great#'
I will get:
''
But I want to get:
'Trump is great#'
So I want to a range of 'Trump is great#', and delete this substring by this range.
How to do that?
Or other ways to delete a substring?
I think what you are looking for is sub!.
Unlike gsub! or delete!, it only replaces the first match.
'Trump is great#Trump is great#'.sub!('Trump is great#', '')
=> 'Trump is great#'
Since it accepts regular expressions, you could use gsub to define how many times you would like for it to match.
If your string is always doubled...
str.gsub!(/^(.*)(?=\1$)/, '')
I find I can do this! :
'Trump is great#Trump is great#'.slice! 'Trump is great#'
If the pattern of your string is similar, you can do something like:
string.split('#').first + '#'
You can also add a custom method to string Class:
class String
def cut_pound
split('#').first + '#'
end
end
So, in case you are having strings with same pattern:
string1 = 'Wimbledon Open#Wimbledon Open#'
string2 = 'FIFA world cup#FIFA world cup#'
Is it possible to call:
string1.cut_pound # => "Wimbledon Open#"
string2.cut_pound # => "FIFA world cup#"
You can remove the pound getting rid of + '#'

How to implement %% to split string like shell in ruby

I am new to ruby and I want to do the following action to remove last "_val3" in ruby:
$ val="val1_val2_val3"
$ echo ${val%_*}
val1_val2
I used to use echo ${val%_*} to get "val1_val2", but i do not how do this in ruby.
Also, how to get "val1"?
Is there a good way to do them?
Not a ruby expert but I'll get the ball rolling with a regular expression:
a.sub /_[^_]*$/, ''
Match an underscore followed by any number of non-underscores at the end of the string. Replace with nothing.
You can use a single gsub to get your expected o/p,
a = "a-b_c_d"
# => "a-b_c_d"
a.gsub /_[a-z]*$/, ''
# => "a-b_c"
Or, you can use ruby split and join,
a.split("_")[0..-2].join("_")
# => "a-b_c"
String#rpartition would probably work:
'a-b_c_d'.rpartition('_') #=> ["a-b_c", "_", "d"]
rpartition looks for the last '_' and returns an array containing the part before it, the separator itself and the part after it.

Break a string by first occurrence of a non-letter character?

I have strings like these:
asdf.123
asdf_123
asdf123
as123df
How could I split by any non-letter character to get this:
asdf
asdf
asdf
as
You could use the String#[] method:
regexp = /^[a-z]+/i
'asdf.123'[regexp]
# => "asdf"
'as123df'[regexp]
# => "as"
'ASas123'[regexp]
# => "ASas"
"your string".split(/[^A-Za-z]/).first
Will split by anything not in A-Z or a-z and then return the first result.
You could simply just replace all non-alpha characters using gsub(/\W+/, '') with a regex expression...
You could simply do:
a = "string 1232"
a[/[a-zA-Z]+/]
# => "string"
This will work for you "aaas._123ff".gsub!(/[^a-zA-Z].*/, '')
=> "aaas"

Regex to get filename / exclude certain characters from the match

Here's my file:
name.extension
And here's my regex:
.*[.]
However this matches the filename and the period:
#=> "filename."
How can I exclude the period in order to achieve:
#=> "filename"
I'm using Ruby.
You can use File class methods File#basename and File#extname:
file= "ruby.rb"
File.basename(file,File.extname(file))
# => "ruby"
You just need a negated character clas:
^[^.]*
This will match everything, from the beginning of the string till it finds a period (but not include it).
Match upto the last "."
"filen.ame.extension"[/.*(?=\.)/]
# => filen.ame
Match upto first "."
"filen.ame.extension"[/.*?(?=\.)/]
# => filen
Alternatively, you can create subgroups in the regexp and just select the first:
str = 'name.extension'
p str[/(.*)[.]/,1] #=> name

matching a string in ruby with regex

I have the following line
'passenger (2.2.5, 2.0.6)'.match(//)[0]
which obviously doesn't match anything yet
I want to return the just the content of (2.2.5, so everything after the open parentheses and before the comma.
How would I do this?
Beanish solution fails on more than 2 version numbers, you should use something like:
>> 'passenger (2.2.5, 2.0.6, 1.8.6)'.match(/\((.*?),/)[1] # => "2.2.5"
'passenger (2.2.5, 2.0.6)'.match(/\((.*),/)[1]
if you use the $1 element it is the group that is found within the ( )
#!/usr/bin/env ruby
s = 'passenger (2.2.5, 2.0.6)'
p s.scan(/(?:\(|, *)([^,)]*)/).flatten # => ["2.2.5", "2.0.6"]

Resources