ruby regex for two backslashes [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 8 years ago.
Improve this question
I am trying to find a regex that matches any string with two consecutive backslashes. I'm trying every variation, any help? And info on handling backslashes
b='\\mystring'
if b=~/\\\\/
puts "it matches"
end
I can't seem to get this to match.

Your regex looks good, but your string does NOT contain two backslashes, it just contains one. It looks like it contains two but that's only in your source file, not in the actual string. Try this:
'\\\\mystring' =~ /\\\\/

Backslash is still a bit magic even inside hard ' quotes. For one thing, it needs to be able to quote ' itself, and so what if your string had a \ followed by '? You need to be able to backslash-quote the backslash in order to unambiguously get a single \ before a magic character, and for sanity, everywhere else as well.
>> '\\'.length
=> 1
>> '\''
=> "'"
>> '\\'[1,1]
=> ""
>> '\\'[0,1]
=> "\\"

Related

Bash grep complicated search [closed]

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.

How to match strings, ignoring certain characters [closed]

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

Double " " or single ' ' quotation marks in Ruby - which is more correct/common? [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 8 years ago.
Improve this question
When used to enclose strings, I'm wondering if one is more correct or common than the other? Do any firms or coding teams insist on one way or another?
Edit:
Since you would use double quotes if you are doing string interpolation, and double quotes also works for variable assigning, eg
str = "string"
Then is it acceptable to only use " " at all times? Then that saves the programmer from having to differentiate between whether to use ' ' or " ".
The difference between " " and ' ' is that ' ' will write exactly what you are typing and with " " you can interpolate and use escaping characters like \n new line
Use double quotes if you are doing string interpolation:
aux = 'World'
"Hello #{aux}"
Use single quote otherwise
You can see Ruby Styleguide in GitHub

Using method sub to transform a string with a var type '$' [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
I'm looking to modify a string as follows:
"one hundred forty-four".sub(/(\w+)(\s)([a-z\-]+)$/){$2 = "test"}
say.rb:78: Can't set variable $2
How can I do that?
edit: one hundredtestforty-four is what i want.
It is not allowed. It is a read only global variable.
Using another global variable name it works fine.
Of course, using global variables bring concerns of clobbering other parts of your program.
I believe you want:
"one hundred forty-four".sub(/\s+([a-z-]+)$/, 'test\1')
#=> "one hundredtestforty-four"
or
"one hundred forty-four".sub(/\s+([a-z-]+)$/, "test\\1")
#=> "one hundredtestforty-four"
or
"one hundred forty-four".sub(/\s+([a-z-]+)$/, "test"+$1)
#=> "one hundredtestforty-four"
or
"one hundred forty-four".sub(/\s+([a-z-]+)$/, "test#{$1}")
#=> "one hundredtestforty-four"
The regex looks for a string that starts with one or more spaces, then any number of lowercase letters or hypens, followed by an end-of-line. (Note the hypen is not escaped within a character class, and it must be appear first or last within the class). It therefore matches " forty-four", with capture group 1 containing "forty-four". Ergo, " forty-four" is replaced with "testforty-four". Notice that you retrieve the contents of capture group 1 by writing \1 if the string is written with single quotes, \\1 if double-quotes are used. Alternatively, you can use the global variable in one of two ways shown.
Note that, if desired, you can use $1 to reference the contents of capture group 1 in subsequent statements.

Multiple action on string in ruby in one go [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
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 ''

Resources