Ruby - how to remove some chars from string? - ruby

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

Related

How do I find any space before "."

I have names "example .png" and "example 2.png". I am trying to convert any space to "_" and any space before "." should be removed.
So far I am doing it like this:
file.gsub(" .",".").gsub(" ", "_").gsub(".tif", "")
Use an rstripped File.basename(filename,File.extname(filename)) and replace spaces with underscores inside it then add an extname:
File.basename(filename,File.extname(filename)).rstrip.gsub(" ", "_") + File.extname(filename)
See the Ruby demo
Details:
File.basename(filename,File.extname(filename)) - get file name without extension
.rstrip - remove whitespace before the extension
.gsub(" ", "_") - replaces spaces (use /\s+/ regex to remove any whitespaces) with underscores
File.extname(filename) - a file extension.
If you prefer a regex way:
s = 'some example 2 .png'
puts s.gsub(/\s+(\.[^.]+\z)|\s/) {
Regexp.last_match(1) ?
Regexp.last_match(1) :
"_"
}
(can be shortened to s.gsub(/\s+(\.[^.]+\z)|\s/) { $1 || "_" } (see Jordan's remark)).
See this Ruby demo.
Here, the pattern matches:
\s+(\.[^.]+\z) - 1 or more whitespaces (\s+) before the extension (\.[^.]+ - a dot followed with 1+ chars other than a dot before the end of string \z), while capturing the extension into Group 1
| - or
\s - any other whitespace symbol (add + after it if you need to replace whole whitespace chunks with underscores).
In the gsub block, a check is performed to test Group 1, and if it matched, only the extension is inserted into the result. Else, a whitespace is replaced with an underscore.

Ruby - How to remove space after some characters?

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&"

Ruby - substitute \n if not \\n

I'm trying to do a regex with lookbehind that changes \n to but not if it's a \\n.
My closest attempt has no effect:
text.gsub /(?<!\\)\n/, ''
Unfortunately, no number of backslashes in the lookbehind seem to fix the problem. How can I address this?
You need to double the backslash before the n in the regex, otherwise it's looking for a newline instead of a literal backslash followed by n:
irb(main):001:0> puts "hello\\nthere\\\\n".gsub(/(?<!\\)\\n/, ' ')
hello there\\n
You don't need anything special. "\n" is a single character. It does not include a "\" or "n" character.
text.gsub(/\n/, "")
But instead of that, you should do:
text.gsub("\n", "")
or
text.tr("\n", "")
But I would do:
text.tr($/, "")

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 replace \r in a string in ruby

I have a string that looks like this.
mystring="The Body of a\r\n\t\t\t\tSpider"
I want to replace all the \r, \n, \t etc with a whitespace.
The code I wrote for this is :
mystring.gsub(/\\./, " ")
But this isn't doing anything to the string.
Help.
\r, \n and \t are escape sequences representing carriage return, line feed and tab. Although they are written as two characters, they are interpreted as a single character:
"\r\n\t".codepoints #=> [13, 10, 9]
Because it is such a common requirement, there's a shortcut \s to match all whitespace characters:
mystring.gsub(/\s/, ' ')
#=> "The Body of a Spider"
Or \s+ to match multiple whitespace characters:
mystring.gsub(/\s+/, ' ')
#=> "The Body of a Spider"
/\s/ is equivalent to /[ \t\r\n\f]/
String#tr is designed for stream symbol substitution. It appears to be a bit quickier, than String#gsub:
mystring.tr "\r", ' '
It hasan insplace version also (this will replace all carriage returns, line feed and spaces with space):
mystring.tr! "\s\r\n\t\f", ' '
Stefen's Answer is really very Cool as always comeup with very short and clean solutions. But here what I tried to remove all special characters. [Posted as just optional solution] ;)
> a = "The Body of a\r\n\t\t\t\tSpider"
=> "The Body of a\r\n\t\t\t\tSpider"
> a.gsub(/[^0-9A-Za-z]/, ' ')
=> "The Body of a Spider"
you can use strip , then add a space to your string
mystring.strip . " "
If you literally has \r\n\t in your string:
mystring="The Body of a\r\n\t\t\t\tSpider"
mystring.split(/[\r\t\n]/)

Resources