Remove All Line Breaks (Enter Symbols) from the String Using R

remove all line breaks (enter symbols) from the string using R

You need to strip \r and \n to remove carriage returns and new lines.

x <- "foo\nbar\rbaz\r\nquux"
gsub("[\r\n]", "", x)
## [1] "foobarbazquux"

Or

library(stringr)
str_replace_all(x, "[\r\n]" , "")
## [1] "foobarbazquux"

How can I remove line break tags from a character vector with Regular Expressions

We can use str_remove_all which would make it compact instead of using the replacement argument in str_replace_all with ""

stringr::str_remove_all(text, '([[:punct:]]|\\n{1,})')

NOTE: str_replace replaces only the first instance and not more than one

How to remove all line breaks from a string

Line breaks (better: newlines) can be one of Carriage Return (CR, \r, on older Macs), Line Feed (LF, \n, on Unices incl. Linux) or CR followed by LF (\r\n, on WinDOS). (Contrary to another answer, this has nothing to do with character encoding.)

Therefore, the most efficient RegExp literal to match all variants is

/\r?\n|\r/

If you want to match all newlines in a string, use a global match,

/\r?\n|\r/g

respectively. Then proceed with the replace method as suggested in several other answers. (Probably you do not want to remove the newlines, but replace them with other whitespace, for example the space character, so that words remain intact.)

Remove all line breaks from a long string of text

How do you enter line breaks with raw_input? But, once you have a string with some characters in it you want to get rid of, just replace them.

>>> mystr = raw_input('please enter string: ')
please enter string: hello world, how do i enter line breaks?
>>> # pressing enter didn't work...
...
>>> mystr
'hello world, how do i enter line breaks?'
>>> mystr.replace(' ', '')
'helloworld,howdoienterlinebreaks?'
>>>

In the example above, I replaced all spaces. The string '\n' represents newlines. And \r represents carriage returns (if you're on windows, you might be getting these and a second replace will handle them for you!).

basically:

# you probably want to use a space ' ' to replace `\n`
mystring = mystring.replace('\n', ' ').replace('\r', '')

Note also, that it is a bad idea to call your variable string, as this shadows the module string. Another name I'd avoid but would love to use sometimes: file. For the same reason.

stringr - remove multiple spaces, but keep linebreaks (\n, \r)

With stringr, you may use \h shorthand character class to match any horizontal whitespaces.

library(stringr)
x <- "hello \n\r how are you \n\r all good?"
x <- str_replace_all(x, "\\h+", " ")
## [1] "hello \n\r how are you \n\r all good?"

In base R, you may use it, too, with a PCRE pattern:

gsub("\\h+", " ", x, perl=TRUE)

See the online R demo.

If you plan to still match any whitespace (including some Unicode line breaks) other than CR and LF symbols, you may plainly use [^\S\r\n] pattern:

str_replace_all(x, "[^\\S\r\n]+", " ")
gsub("[^\\S\r\n]+", " ", x, perl=TRUE)

regex remove line breaks after any but specific characters

$(?<=[^;])(?<=[^"])\R

  • $ Find end of line
  • (?<=[^;]) Must not end with ;
  • (?<=[^"]) Must not end with "
  • \R Match linebreak character(s)

How to remove a line break between two strings (RegEx)

Use the below regex and then replace the match with a single space.

(?s)\s+(?=(?:(?!%}|\{%).)*%\})

DEMO

Explanation:

(?s)                     set flags for this block (with . matching
\n) (case-sensitive) (with ^ and $
matching normally) (matching whitespace
and # normally)
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times)
(?= look ahead to see if there is:
(?: group, but do not capture (0 or more
times):
(?! look ahead to see if there is not:
%} '%}'
| OR
\{ '{'
% '%'
) end of look-ahead
. any character
)* end of grouping
% '%'
\} '}'
) end of look-ahead


Related Topics



Leave a reply



Submit