Replace special characters in string [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 7 years ago.
Improve this question
I have strings like
"Ruby & Rails"
"Ruby& Rails"
"Ruby !Rails"
I want to convert them to "Ruby-Rails". How can I do this?

Assuming 1,2,3 are serial number for example and not are actually present in String, you can use split and join:
a = "Ruby & Rails"
a.split(/\W+/).join("-")
# => "Ruby-Rails"
"Ruby& Rails".split(/\W+/).join("-")
# => "Ruby-Rails"
"Ruby !Rails".split(/\W+/).join("-")
# => "Ruby-Rails"
Alternatively if serial number is also part of input string:
"1. Ruby& Rails".split(/\W+|\d+/).reject(&:empty?).join("-")
# => "Ruby-Rails"
"2. Ruby& Rails".split(/\W+|\d+/).reject(&:empty?).join("-")
# => "Ruby-Rails"
"3. Ruby !Rails".split(/\W+|\d+/).reject(&:empty?).join("-")
# => "Ruby-Rails"

You could use gsub.
string.gsub(/\s*\W+\s*/, "-")
OR
string.gsub(/\W+/, "-")

" Ruby ! Rails ".split(/\W+/).reject(&:empty?).join("-")
=> "Ruby-Rails"

Related

How to get a string with `"\y"` [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 4 years ago.
Improve this question
I have a password that contains "\y". I get:
"a\yb" # => "ayb"
'a\yb' # => 'a\\yb'
"a\\yb" # => "a\\yb"
'a\\b' # => "a\\yb"
And nothing (like concatenation or sub) works.
Is there a way to not change the password?
When you say:
'a\yb'
Then you get this back:
"a\\yb"
These are identical as far as Ruby is concerned. Inside of double quotes (") the backslash has special meaning. A single backslash is used to indicate either control codes like \n meaning newline, or literal versions of same, like \\ meaning literal backslash.
Try this:
puts "a\\yb"
Then you'll see exactly what you want. The \\ part is just escaping.
When you use p you're calling:
puts "a\\yb".inspect
This inspect part puts it back into a double-quoted and escaped string, which is where you're getting confused. Print the string, not the "inspected" version of it.
Works for me. Can you show us some context?
ruby -v
#=> ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-linux]
ruby -e 'puts "a\\yb"'
#=> a\yb
After your edits, the problem is using p instead of puts. For instance:
ruby -e 'puts "a\\yb"'
#=> a\yb
ruby -e 'p "a\\yb"'
#=> "a\\yb"
See this discussion.
In simple words p print obj.inspect to output
Simple way to see this:
irb(main):009:0> string = %q{a\yb}
=> "a\\yb"
irb(main):018:0> puts string.inspect
"a\\yb"
irb(main):019:0> p string
"a\\yb"
irb(main):010:0> puts string
a\yb

Introducing backslash and quote at beginning of string [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 want to convert "her" to "\"her".
I've tried using insert method:
>> "her".insert(0,'\"')
=> "\\"her"
and
>> "her".insert(0,'"')
=> ""her"
None of them gives me what I want: "\"her"
"her".insert(0,'"')
actually returns "\"her", which is what you said you wanted in the first place.
If you want to obtain "\"her\"", you might want to use Object#inspect:
"her".inspect
=> "\"her\""
Or, you can simply concatenate quotes at the beginning and at the end:
'"' + "her" + '"'
=> "\"her\""
If you just want "\"her"
a = "her".insert(0,'\"')
#=> "\\\"her"
puts a
#=> \"her
If you want output "\"her\""
a = "her"
#=> "her"
b = '\"'+a+'\"'
#=> "\\\"her\\\""
puts b
#=> \"her\"
I think your code is fine, you just don't know the meaning of \\ in console.
The first \ is escape character and second \ is character itself.
You will see \"her in the text foo.txt as you expected by:
File.write("foo.txt", "her".insert(0,'\"'))

Ruby regex with variable [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 want to have a regular expression that works with a variable in it
Right now it looks like var=status.text[/.*#{keyword}.*is now available.*/io]
but the variable breaks the regular expression
What do you mean by 'breaks'?
It works out of the box
[36] pry(main)> a = 1
=> 1
[37] pry(main)> /#{a}/
=> /1/
You can put the variable in a string first, then convert the string to a regexp as follows:
[4] pry(main)> keyword = "cat"
=> "cat"
[5] pry(main)> my_regexp = Regexp.new(".*#{keyword}.*is now available.*")
=> /.*cat.*is now available.*/

Regular expression inquiry [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 using Ruby to do a simple substring type match. I'm trying to understand why the following happens:
irb(main):024:0> puts "match" if "foo" =~ /foo/
match
=> nil
irb(main):025:0> puts "match" if "foo" =~ /foo,/
=> nil
how can this regex be modified so that if any part of the search criteria matches "foo", a match is made?
You've got your comparisons backwards:
"foo".match(/foo,/) # See if "foo" matches the pattern "foo,"
# => nil
"foo,".match(/foo/) # See if "foo," matches the pattern "foo"
# => #<MatchData "foo">
The =~ operator is a bit of history that has fallen out of style because it's not self-explanatory.
You can use the scan method of String and pass in the regex you want to check against.
1.9.3p194 :008 > puts "match" if "foo".scan(/foo,/)
match
=> nil

How can I do the following regex in 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 9 years ago.
Improve this question
how do i match each of the following with regex ?
some text includes [any number of character]. need to pull the entire [asdfasdf]
#tableid='sometext' I need to pull the sometext
mary and drugs I need to pull " and ", spaces included.
irb(main):002:0> "some text include [abcdef]".match(/\[(.*)\]/)[1]
=> "abcdef"
irb(main):005:0> "#table_id='2356'".match(/'(.*)'/)[1]
=> "2356"
irb(main):006:0> "mary and drugs".match(/mary(.*)drugs/)[1]
=> " and "
Try these:
"\[(.*?)\]"
"#\w+='(.*?)'"
" +and +"

Resources