Regular expression in ruby 2.1.5 [closed] - ruby

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 want to extract alpha-beta from:
text = "alpha-beta-1.0.txt"
to get:
output = "alpha-beta"
Can somebody here help me with a regex?

If you have a computer programming problem, and you think, "I'll use a
regex!", now you have two problems.
Here it is without a regex:
strings = [
"alpha-beta-1.0.txt",
"alpha-beta-theta-2.0.txt",
"alpha-3.0.text",
]
strings.each do |string|
output = string.rpartition('-')[0]
puts output
end
--output:--
alpha-beta
alpha-beta-theta
alpha

If you want to extract just before last -, you can use this regex
(^.*)-(?:.*$)
Rubular Demo
For finding all non-overlapping matches, you can use scan as
str = "alpha-beta-1.0.txt"
print str.scan(/(^.*)-(?:.*$)/)[0][0]
Ideone Demo
You can also use lookahead as
.*(?=-)

With the information we have i'd propose:
output = "alpha-beta-1.0.txt".match(/(.*-.*)-.*/)[1]
.* matches a lot though. So perhaps you need a more restrictive match.

Related

Is there a better way to Regex match a string of digits between the range of 2-100 than this? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
This is my attempt, is there a better way?
^([2-9]|[1-9][0-9]|100)$
The better way could be not to use regex.
puts 'in range' if (2..100) === '20'.to_i
You regex is not correct for Ruby. In Ruby, ^ matches at the beginning of a line, not the beginning of the string; similarly for $ at the end of line not string.
In Ruby, ^ and $ are almost always a mistake, you generally want to use \A and \z (respectively) instead:
\A([2-9]|[1-9][0-9]|100)\z
If you want or need to use a regex, start with a regex that matches what you're looking for.
It depends.
If that regex is just a piece of a bigger regexp or the regexp is kind of a parameter for passing to a function that just accepts a regexp then I don't think It can be better than that.
However, if you just use this for validating some numbers, then a much better approach is to cast the text to an integer a then validate with >= 2 and <=100 or between?
It depends on what your context is and if you are concerned about performance.
I would do conversion to int:
str =~ /^(\d+)$/ && $1.to_i.between?(2, 100)
I think it is much easier to read than your regex. Basically, it says: "str is just a number and this number is between 2 and 100".
And this test is fully compatible with yours (equivalent)

Need to fetch the password using Regular expression [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 6 years ago.
Improve this question
a= <<EOF
Password
:
7UV1ceFQ (You will be asked to change this after logging in for the first time)
EOF
I need to extract the value "7UV1ceFQ" using regular expression, I have tried using '/Password : 7UV1ceFQ/ but it's not working, I think it's because next line character is included, Can anyone please suggest me to exact this value?
▶ a[/^\S+(?=\s\(You will be)/]
#⇒ "7UV1ceFQ"
The regular expression above reads as:
starting with a new line start ^
get all non-space symbols greedy \S+
until the positive lookahead (?=\s\(You will be)

Regex issue with a string and all numbers [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 8 years ago.
Improve this question
In Ruby, I would like to create a regular expression that matches the following:
building/liberty-green/6d
(the word building and some number somewhere after it)
Currently, I have /building/ and need to add \d (any digit) to it, but I don't know how.
You need /building\/[\w-]+\/\w+/. For example:
irb(main):001:0> /building\/[\w-]+\/\w+/.match("building/liberty-green/6d")
=> #<MatchData "building/liberty-green/6d">
That expression will match any string that:
Starts with /building/
Then follows with one or more word characters or dashes (eg. foo-bar, foo, bar-1)
Then follows with a /
Finally ends with one or more word characters (eg. foo, 6d, 12345)
Note that \w includes digits.

Regex for words and spaces? [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 9 years ago.
Improve this question
I'm trying to create a regex to match words and spaces:
"Hello There" - Match
"#Hello" - No Match
"123Hello" - No Match
It should something like this:
( *[a-zA-Z]* *)
I need to define a method starts_with_consonant?(s) that takes a string and returns true if it starts with a consonant and false otherwise.
For our purposes, a consonant is any letter other than 'A', 'E', 'I', 'O', 'U'.
This is for a course.
This seems to work:
(?<=^|\s)[a-zA-Z]+(\s+[a-zA-Z]+)*(?=\s|$)
See a live demo of this working with your examples (and others)
If you want to match only if the whole input matches, swap the look arounds for ^ and $:
^[a-zA-Z]+(\s+[a-zA-Z]+)*$
See a live demo of this.
I guess this does almost what you want:
\A(\w*\s+\w+)+\Z

Convert string using Ruby [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 8 years ago.
Improve this question
How can I convert the string russ(ai)(edocn)cup to russiancodecup using Ruby?
By using gsub with a block, you can replace any match of a regular expression by the result of this block.
s = "russ(ai)(edocn)cup"
s.gsub(/\(([^)]*)\)/) {$1.reverse} # => "russiancodecup"
Here the regular expression will match any non-) character between brackets. Then it will send reverse to $1 which is gonna be the content between brackets.
$0 will be the complete match and $n, the nth "submatch". (anybody for the correct word ?)

Resources