replace every other space with new line

cory

I have strings like this:

a <- "this string has an even number of words"
b <- "this string doesn't have an even number of words"

I want to replace every other space with a new line. So the output would look like this...

myfunc(a)
# "this string\nhas an\neven number\nof words"
myfunc(b)
# "this string\ndoesn't have\nan even\nnumber of\nwords"

I've accomplished this by doing a strsplit, paste-ing a newline on even numbered words, then paste(a, collapse=" ") them back together into one string. Is there a regular expression to use with gsub that can accomplish this?

Frank

@Jota suggested a simple and concise way:

myfunc  = function(x) gsub("( \\S+) ", "\\1\n", x)       # Jota's    
myfunc2 = function(x) gsub("([^ ]+ [^ ]+) ", "\\1\n", x) # my idea

lapply(list(a,b), myfunc)


[[1]]
[1] "this string\nhas an\neven number\nof words"

[[2]]
[1] "this string\ndoesn't have\nan even\nnumber of\nwords"

How it works. The idea of "([^ ]+ [^ ]+) " regex is (1) "find two sequences of words/nonspaces with a space between them and a space after them" and (2) "replace the trailing space with a newline".

@Jota's "( \\S+) " is trickier -- it finds any word with a space before and after it and then replaces the trailing space with a newline. This works because the first word that is caught by this is the second word of the string; and the next word caught by it is not the third (since we have already "consumed"/looked at the space in front of the third word when handling the second word), but rather the fourth; and so on.

Oh, and some basic regex stuff.

  • [^xyz] means any single char except the chars x, y, and z.
  • \\s is a space, while \\S is anything but a space
  • x+ means x one or more times
  • (x) "captures" x, allowing for reference in the replacement, like \\1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Replace Space with New Line in Python

From Dev

bash - replace space with new line

From Dev

Notepad++ - replace every first space of a line

From Dev

how to replace every 6th ooccurrence blank space with new line from a file?

From Dev

How do I replace new line and space with <td><tr> using sed for every calendar month?

From Dev

Replace every other occurence on the same line

From Dev

Replace \n(new line) with space in bash

From Dev

how to replace new line to empty space on coldfusion

From Dev

Using sed or VIM to replace space with new line

From Dev

How do I replace a space with a new line?

From Dev

Replace new line + 4x space to new line in stdout

From Dev

Space and new line after every n'th character

From Dev

How to make a new line for every space, except one?

From Dev

Replace new line character between double quotes with space

From Dev

Process substitution removes new-line and replace with space?

From Dev

Replace blank space with new line in txt file with Python

From Dev

Replace every nth instance of a character with a new line in Notepad++

From Dev

Replace every odd occurrence of space with “_”

From Dev

Replace every odd occurrence of space with “_”

From Dev

How to convert row to two column in notepad++ (Replace every second 'space' to 'new row')

From Dev

In Notepad++ , how to replace with new line, every last occurrence of a pattern in a line?

From Dev

Replace the first 3 white space coming with a character and ignore other white spaces in a text line with python

From Dev

Replace a character with new line

From Dev

Interchange every other line with awk

From Dev

Regex PHP to find and replace white space and,or new line between HTML tags

From Dev

Google Spreadsheets / Excel: Replace every nth Space

From Dev

Python replace every odd occurrence of space with "_"

From Dev

Save Space Using Just Every Other P?

From Dev

Adding a space after every other digit in java

Related Related

  1. 1

    Replace Space with New Line in Python

  2. 2

    bash - replace space with new line

  3. 3

    Notepad++ - replace every first space of a line

  4. 4

    how to replace every 6th ooccurrence blank space with new line from a file?

  5. 5

    How do I replace new line and space with <td><tr> using sed for every calendar month?

  6. 6

    Replace every other occurence on the same line

  7. 7

    Replace \n(new line) with space in bash

  8. 8

    how to replace new line to empty space on coldfusion

  9. 9

    Using sed or VIM to replace space with new line

  10. 10

    How do I replace a space with a new line?

  11. 11

    Replace new line + 4x space to new line in stdout

  12. 12

    Space and new line after every n'th character

  13. 13

    How to make a new line for every space, except one?

  14. 14

    Replace new line character between double quotes with space

  15. 15

    Process substitution removes new-line and replace with space?

  16. 16

    Replace blank space with new line in txt file with Python

  17. 17

    Replace every nth instance of a character with a new line in Notepad++

  18. 18

    Replace every odd occurrence of space with “_”

  19. 19

    Replace every odd occurrence of space with “_”

  20. 20

    How to convert row to two column in notepad++ (Replace every second 'space' to 'new row')

  21. 21

    In Notepad++ , how to replace with new line, every last occurrence of a pattern in a line?

  22. 22

    Replace the first 3 white space coming with a character and ignore other white spaces in a text line with python

  23. 23

    Replace a character with new line

  24. 24

    Interchange every other line with awk

  25. 25

    Regex PHP to find and replace white space and,or new line between HTML tags

  26. 26

    Google Spreadsheets / Excel: Replace every nth Space

  27. 27

    Python replace every odd occurrence of space with "_"

  28. 28

    Save Space Using Just Every Other P?

  29. 29

    Adding a space after every other digit in java

HotTag

Archive