Ruby - How to remove space after some characters? - ruby

I need to remove white spaces after some characters, not all of them. I want to remove whites spaces after these chars: I,R,P,O. How can I do it?

"I ".gsub(/(?<=[IRPO]) /, "") # => "I"
"A ".gsub(/(?<=[IRPO]) /, "") # => "A "

" P $ R 3I&".gsub(/([IRPO])\s+/,'\1')
#=> " P$ R3I&"

Related

Replacing escape quotes with just quotes in a string

So I'm having an issue replacing \" in a string.
My Objective:
Given a string, if there's an escaped quote in the string, replace it with just a quote
So for example:
"hello\"74" would be "hello"74"
simp"\"sons would be simp"sons
jump98" would be jump98"
I'm currently trying this: but obviously that doesn't work and messes everything up, any assistance would be awesome
str.replace "\\"", "\""
I guess you are being mistaken by how \ works. You can never define a string as
a = "hello"74"
Also escape character is used only while defining the variable its not part of the value. Eg:
a = "hello\"74"
# => "hello\"74"
puts a
# hello"74
However in-case my above assumption is incorrect following example should help you:
a = 'hello\"74'
# => "hello\\\"74"
puts a
# hello\"74
a.gsub!("\\","")
# => "hello\"74"
puts a
# hello"74
EDIT
The above gsub will replace all instances of \ however OP needs only to replace '" with ". Following should do the trick:
a.gsub!("\\\"","\"")
# => "hello\"74"
puts a
# hello"74
You can use gsub:
word = 'simp"\"sons';
print word.gsub(/\\"/, '"');
//=> simp""sons
I'm currently trying str.replace "\\"", "\"" but obviously that doesn't work and messes everything up, any assistance would be awesome
str.replace "\\"", "\"" doesn't work for two reasons:
It's the wrong method. String#replace replaces the entire string, you are looking for String#gsub.
"\\"" is incorrect: " starts the string, \\ is a backslash (correctly escaped) and " ends the string. The last " starts a new string.
You have to either escape the double quote:
puts "\\\"" #=> \"
Or use single quotes:
puts '\\"' #=> \"
Example:
content = <<-EOF
"hello\"74"
simp"\"sons
jump98"
EOF
puts content.gsub('\\"', '"')
Output:
"hello"74"
simp""sons
jump98"

How to use Ruby's gsub function to replace excessive '\n' on a string

I have this string:
string = "SEGUNDA A SEXTA\n05:24 \n05:48\n06:12\n06:36\n07:00\n07:24\n07:48\n\n08:12 \n08:36\n09:00\n09:24\n09:48\n10:12\n10:36\n11:00 \n11:24\n11:48\n12:12\n12:36\n13:00\n13:24\n13:48 \n14:12\n14:36\n15:00\n15:24\n15:48\n16:12\n16:36 \n17:00\n17:24\n17:48\n18:12\n18:36\n19:00\n19:48 \n20:36\n21:24\n22:26\n23:15\n00:00\n"
And I'd like to replace all \n\n occurrences to only one \n and if it's possible I'd like to remove also all " " (spaces) between the numbers and the newline character \n
I'm trying to do:
string.gsub(/\n\n/, '\n')
but it is replacing \n\n by \\n
Can anyone help me?
The real reason is because single quoted sting doesn't escape special characters (like \n).
string.gsub(/\n/, '\n')
It replaces one single character \n with two characters '\' and 'n'
You can see the difference by printing the string:
[302] pry(main)> puts '\n'
\n
=> nil
[303] pry(main)> puts "\n"
=> nil
[304] pry(main)> string = '\n'
=> "\\n"
[305] pry(main)> string = "\n"
=> "\n"
I think you're looking for:
string.gsub( / *\n+/, "\n" )
This searches for zero or more spaces followed by one or more newlines, and replaces the match with a single newline.

Ruby - how to remove some chars from string?

I have following strings:
" asfagds gfdhd"sss dg "
"sdg "dsg "
desired output:
asfagds gfdhd"sss dg
sdg "dsg
(Empty spaces removed from the front and end of the strings, as well as leading and trailing double quotes.)
I have a big file with these lines and I need them format to our needs... How could I remove the " from the start and end of the respective file and remove the white spaces from the start and end of the file?
Use string.strip or string.strip!.
" asfagds gfdhd\"sss dg ".strip
"asfagds gfdhd\"sss dg"
Be aware that strip removes all whitespaces (fe. tabs, newlines), not just spaces.
If you want to remove just spaces use:
string.gsub /^ *| *$/, ''
If you want to remove " as well:
string.gsub /^" *| *"$/, ''
If the data in the file is clean and uniform, then this should do
'" asfagds gfdhd"sss dg "'[1..-2].strip
If the data is not clean, you may need to do a strip before too.. (ie if there are trailing spaces after the closing quotation marks.
'" asfagds gfdhd"sss dg "'.strip[1..-2].strip
Really depends on how clean the data in the file is.
Use strip:
" Hello World ".strip #=> "Hello World"
Or to only strip from the left/right use lstrip and rstrip respectively.
One liner:
irb> '" asfagds gfdhd"sss dg "'[1..-2].strip
=> "asfagds gfdhd"sss dg"
take the [1,n-1] substring, remove whitespace

Regex to match any character except a backslash

How can i say "all symbols except backslash" in Ruby character class?
/'[^\]*'/.match("'some string \ hello'") => should be nil
Variant with two backslashed doesn't work
/'[^\\]*'/.match("'some string \ hello'") => 'some string \ hello' BUT should be nil
Your problem is not with your regex; you got that right. Your problem is that your test string does not have a backslash in it. It has an escaped space, instead. Try this:
str = "'some string \\ hello'"
puts str #=> 'some string \ hello'
p /'[^\\]*'/.match(str) #=> nil
You need to escape the backslash:
[^\\]*
because backslash is the escape character in regular expressions, thus escaping the closing bracket here.
If you want to verify that the whole string contains non-backslash characters, then you need anchors:
^[^\\]*$
There's actually not a backslash in your string to match against. Try taking a look at just your input:
"'some string \ hello'".length # => 20
"a\ b".length # => 3
The "\ " in double quotes is being escaped into just a space:
irb(main):014:0> " "[0].to_i # => 32
irb(main):015:0> "\ "[0].to_i # => 32
irb(main):016:0> "\ ".size #=> 1
If you want to match no slash, you'll need two, like in your second example, which looks good to me:
/'[^\\]*'/.match("'some string \\ hello'") # => nil
This is relevant to Csharp.. but idk it might help you.
[^\\\\]*
four slashes instead of two.

Replace a character with \1

I'm trying to do something like the following. Let's say i have the following string:
"some string"
And i wanted to replace the space with a \1. However, wether i use single or double quote, i dont get the result:
"some string".gsub(" ", "\1") => "somestring"
"some string".gsub(" ", '\1') => "somestring"
"some string".gsub(" ", '\\1') => "somestring"
What i want is:
"some\1string"
Any suggestions?
This is only annoying because \1 through \9 are reserved for use in substitutions.
A possible solution is:
"some string".gsub(" ", '\\\\1')
It's kind of ugly, but it works.
An alternative is to use the block style where substitution isn't performed:
"some string".gsub(" ") { '\1' }
Remember that the output will be "some\\1string" because backslash is represented as \\ inside a double-quoted string. If you print it out you will get a single backslash.
This is also possible:
s = "some string"
p s.split.join('\1') # "some\\1string"

Resources