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 7 months ago.
Improve this question
I am trying to figure out the Ruby Regex for the exact string "and/or". For example, let's say I have a name variable that is "Elvin and/or Jafarli"
name = "Elvin and/or Jafarli"
and I want to split the name based on the string "and/or". How is that done in Ruby?
This is the final result I am looking for:
name.split(some_regex) results in ["Elvin", "Jafarli"]
** UPDATE **
This is the current regex that exists in the system
names.split(/ (?i)(?:and|or) /)
What I want to do is to update the regex to also split on exactly string match like "Elvin and/or Jafarli".
Add another alternative with |, and escape the delimiter:
names.split(/ (?i)(?:and|or|and\/or) /)
or use the alternative regex literal form:
names.split(%r{ (?i)(?:and|or|and/or) })
I feel like there must be a catch. This seems too easy.
irb(main):001:0> name = "Elvin and/or Jafarli"
=> "Elvin and/or Jafarli"
irb(main):002:0> name.split /\s+and\/or\s+/
=> ["Elvin", "Jafarli"]
Remember to escape the / in the regular expression and account for the surrounding whitespace. \s+ specifies one or more whitespace characters.
Related
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)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have to match pairs of strings, ignoring spaces " " and hyphens "-". I want to regard the following pairs as identical.
"2,3 chloro benzene" and "2,3 chlorobenzene"
"4'3',2-dinitrotoluene" and "4'3',2-di nitro toluene"
Due to the spaces, I cannot match them. How can I do that? I am not sure how to do it in Ruby.
Use String#delete to delete unwanted chars and normalize the two strings before comparing them, as shown below:
s1 = "2,3 chloro-benzene"
s2 = "2,3 chlorobenzene"
s1.delete(" -") == s2.delete(" -")
#=> true
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
Here is a string a content is "hi all I'm new here(seriously)"
How can I return a "hi+all+I'm+new+here" using ruby code?
Why not simply chain the .gsub() commands?
x.gsub(/\(.*?\)/, '').gsub(/\s+/,'+')
Also, you can update your first gsub to delete any whitespace preceding the brackets aswell.
x.gsub(/\s+\(.*?\)/, '')
If you really want to use a single gsub operation, you can pass a hash as the replacement parameter:
x.gsub(/( *\(.*?\)| )/, ' ' => '+', default: '')
# => "hi+all"
What this does is captures either something in brackets (including the leading spaces) or spaces. If the capture is a space - it is replaced by '+', otherwise, it replaces to empty string ''
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.
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 having some problems coming up with a regexp that matches class1, class2, and class3 in the following string (ideally I could have n number of words separated by pipes, as the number of classes passed to my method is not constant)
class1|class2|class3 path/to/resource
I have the following matcher which returns only class1. Bonus points to whomever can find me a matcher for the resource path as well.
Edit
Thank you very much for all the help - points all around!
Assuming you are confident that your input will be well formed, my advice would be to split your string by both the pipe character and space. For example:
components = "class1|class2|class3 path/to/resource".split(/[ \|]/)
You would then have access to an array containing n components followed by the path to your resource which you can manipulate to access.
resourcePath = components.pop()
classes = components
EDIT: The original topic of this was suggested the OP is using Ruby, hence my answer.
\w+(\|\w+)*\s+\w+(\/\w+)*
I assumed that the names of your classes consist of one or more word characters. Adjust if they're more restricted than that. For instance, use class\d+ for numbered classes only.
We have a class name, followed by any number of [a pipe followed by a class name]. Then we have one or more spaces, followed by basically the same thing, but this time using slashes instead of pipes.
I've escaped both the pipe and the slash with a backslash.
string = "class1|class2|class3 path/to/resource".split(%r{[| ]})
=> ["class1", "class2", "class3", "path/to/resource"]
I would just do two splits:
string = 'class1|class2|class3 path/to/resource'
p string.split.first.split('|') #=> ["class1", "class2", "class3"]
If you want to use regex with the input you provided, this will extract your classes and path:
([\w/]+)\|? ?
INPUT
class1|class2|class3 path/to/resource
OUTPUT
class1
class2
class3
path/to/resource