Replacing White Space with One Single Backslash

Replacing white space with one single backslash

Your problem is a confusion between the content of a string and its representation. When you print out a string in the ordinary way in R you will never see a single backslash (unless it's denoting a special character, e.g. print("y\n"). If you use cat() instead, you'll see only a single backslash.

x <- "foo bar"
y <- gsub(" ", "\\\\ ", x)
print(y)
## [1] "foo\\ bar"
cat(y,"\n") ## string followed by a newline
## foo\ bar

There are 8 characters in the string; 6 letters, one space, and the backslash.

nchar(y)  ## 8

For comparison, consider \n (newline character).

z <- gsub(" ", "\n ", x)
print(z)
## [1] "foo\n bar"
cat(z,"\n")
## foo
## bar
nchar(z) ## 8

If you're constructing file paths, it might be easier to use forward slashes instead - forward slashes work as file separators in R on all operating systems (even Windows). Or check out file.path(). (Without knowing exactly what you're trying to do, I can't say more.)

R: How to replace space (' ') in string with a *single* backslash and space ('\ ')

Get ready for a face-palm, because this:

> gsub(" ", "\\\ ", "a b", fixed = TRUE)
[1] "a\\ b"

is actually working.

The two backslashes you see are just the R console's way of displaying a single backslash, which is escaped when printed to the screen.

To confirm the replacement with a single backslash is indeed working, try writing the output to a text file and inspect yourself:

f <- file("C:\\output.txt")
writeLines(gsub(" ", "\\", "a b", fixed = TRUE), f)
close(f)

In output.txt you should see the following:

a\b

Replacing everything with a backslash till next white space

Does that work for you?

re.sub(
r"\\\w+\s*", # a backslash followed by alphanumerics and optional spacing;
'', # replace it with an empty string;
input_string # in your input string
)

>>> re.sub(r"\\\w+\s*", "", r"\fs24 hello there")
'hello there'
>>> re.sub(r"\\\w+\s*", "", "hello there")
'hello there'
>>> re.sub(r"\\\w+\s*", "", r"\fs24hello there")
'there'
>>> re.sub(r"\\\w+\s*", "", r"\fs24hello \qc23424 there")
'there'

how to replace single backslash with empty character in string?

Back Slash itself is an escape Character,so you need to use double backslash to achieve it,as in

message=message.replaceAll("\\","");

Replace forward slash with space before and after only if no space in C#

You can use

var output = Regex.Replace(text, @"\s*(?<!^\s*)/(?!\s*$)\s*", " / ", RegexOptions.Multiline);

See the regex demo

Details:

  • \s* - zero or more whitespaces
  • (?<!^\s*) - a negative lookbehind that fails the match if there is start of a line followed with zero or more whitespaces immediately on the left
  • / - a / char
  • (?!\s*$) - a negative lookahead that fails the match if there are zero or more whitespaces followed with end of line position immediately on the right
  • \s* - zero or more whitespaces.

Or, another variation:

var output = Regex.Replace(text, @"(?<=(?<!^)/(?!\s|$))|(?=(?<!^|\s)/(?!$))", " ");

See this regex demo.

Details:

  • (?<=(?<!^)/(?!\s|$)) - a position right after a / that is neither at the start nor at the end of a string and that is not followed with a whitespace
  • | - or
  • (?=(?<!^|\s)/(?!$)) - a position right before a / that is neither at the start nor at the end of a string and that is not preceded with a whitespace.

Replace space in a Java string by backslashe

Do it like this:

String text = " 1 2 3 4 5 6 7 8 9 10";
text = text.replace(" ","\\\\");
System.out.println(text2);

A character preceded by a backslash (\) is an escape sequence and has special meaning to the compiler.
'\\' Insert a backslash character in the text at this point.

You can read more here: http://docs.oracle.com/javase/tutorial/java/data/characters.html

How to replace whitespace of varying length with one space?

We may need to use + i.e. one or more instead of just [[:space:]]

df$column1 <- gsub("[[:space:]]+", "\n", df$column1) 
df$column1
[1] "A\nB" "C\nD" "E\nF" "G\nH" "I\nJ" "K\nL" "M\nN"

If it is a single space, change the \n to " "

df$column1 <- gsub("[[:space:]]+", " ", df$column1) 
df$column1
[1] "A B" "C D" "E F" "G H" "I J" "K L" "M N"

The OP's code with [[:space:]] result in replacing the space with either space or \n

> gsub("[[:space:]]", "\n", df$column1)
[1] "A\nB" "C\n\nD" "E\n\n\nF" "G\n\n\n\nH" "I\n\n\n\n\nJ" "K\n\n\n\n\n\nL" "M\n\n\n\n\n\n\nN"

How to replace spaces and slash in string in bash?

The following one-liner gives the desired result:

echo "$foo" | tr '\n' '\r' | sed 's,\s*\\\s*, ,g' | tr '\r' '\n'
Hello World!

we are friends

here we are

Explanation:

tr '\n' '\r' removes newlines from the input to avoid special sed behavior for newlines.

sed 's,\s*\\\s*, ,g' converts whitespaces with an embedded \ into one space.

tr '\r' '\n' puts back the unchanged newlines.

Replace any number of white spaces with a single white space

You cannot use a regular expression in the replace() method for strings, you have to use the re module:

import re
mystring = re.sub(r'\s+', ' ', mystring)

Note the r prefix before the string literal, this makes sure that the backslash in your regular expressions is interpreted properly. It wouldn't actually make a difference here, but for different escape sequences it can cause serious problems. For example '\b' is a backspace character but r'\b' is a backslash followed by a 'b', which is used for matching word boundaries in regex.



Related Topics



Leave a reply



Submit