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
Related
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.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
How can I write a searching command using grep that will look for a line with a strict requirements. For example it should start with a name, which consist only letters and "-", then follows an ":", then a year or "xxxx", then again an ":", and then a line of letters, digits and "-" of some length. Or may be there is a link where I can read this... I'm trying to find some solution in the Internet for a long time, but can't...
What you need here is to pass the grep command a regular expression that describes your pattern of interest, on the basis of which grep will match only valid lines.
Taking into account your indications, the following regular expression could do the job:
^([A-z]|-)+:([0-9]|xxxx)+:([A-z]|[0-9]|-)+$
The expression begins and ends with the ^ and $ anchors, that indicate the beginning and the end of a line. Then, you basically have three token blocks, separated by :, the first matching letters and dashes, the second years or xxxx, and the third letters, digits and dashes. + is a quantifier, indicating that the preceding token can appear one or more times.
You can use it with grep like so:
grep -P "^([A-z]|-)+:([0-9]|xxxx)+:([A-z]|[0-9]|-)+$"
The -P option is to indicate to interpret it as a Perl regex and correctly handle hyphens matching.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I've been trying to match only specific prefix and it's extension in a filename but with no luck.
Example:
BX-ST123456.mxf ==> I need to match only BX-ST and .mxf
BX-SR123456.mxf ==> this shouldn't be a match
BXL-ST123456.mxf ==> this shouldn't be a match
so basically the match should happen only if it finds the string of BX-ST and .mxf without restricting the length of numbers in the middle (ignore them) they could be 0-9 in any random.
Example:
BX-ST0.mxf ==> should be a match
BX-ST01.mxf ==> should also be a match and so on.
appreciate your help everyone
Try
BX-ST\d+.mxf
You could also add ^ in the beginning and $ at the end like this
^BX-ST\d+.mxf$
A good site to try your Regex is Rubular
There is a nice class called File that can help you. Such that:
File.fnmatch?('**BX-ST*.mxf','path/to/file/BX-ST123456.mxf')
#=> true
Here the pattern is a glob pattern not a regex and simply says match directory recursively '**', filename starts with "BX-ST" and ends with ".mxf".
Additionally you could go with
File.extname(YOUR_FILE) == '.mxf' && File.basename(YOUR_FILE)[0,5] == 'BX-ST'
Here we check that the extension is ".mxf" and that the basename's first 5 characters are "BX-ST"
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.
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.