Delete the pattern in front of the numbers and save these numbers

I have a line

text = "Math\n      \n      \n        600 rubles / 45 min."
text2 = "Math\n      \n      \n        in a group"

And I want to replace \n \n \nwith " "only in the case of the following digits. As a result, I want to have:

"Math 600 rubles / 45 min."
"Math\n      \n      \n        in a group"

I tried gsub("\n \n \n [\\d]", " ", text), but it also replaces the first digit.

+4
source share
3 answers

You can use a pattern that matches 3 occurrences \nfollowed by 6 + spaces, and then commit the digit and replace the backreference with group 1:

gsub("(?:\n {6,}){3}(\\d)", " \\1", text)

See R demo

More details

  • (?:\n {6,}){3} - 3 consecutive appearances:
    • \n - new line
    • {6,} - 6 or more spaces
  • (\\d)- Group 1 (indicated \1from the replacement template): any digit.
+3
source

I came up with the following pattern:

gsub("\\n[[:blank:]]*\\n[[:blank:]]*\\n[[:blank:]]*(\\d+)", " \\1", text)

, , . . ( , ).

, gsub, , . , [\\d] . , , .

+3
text =c("Math\n      \n      \n        600 rubles / 45 min.","Math\n      \n      \n        in a group")
gsub('((\n\\s+){1,})(?=\\d)',' ',text,perl=T)
#[1] "Math 600 rubles / 45 min."                "Math\n      \n      \n        in a group"
+2
source

Source: https://habr.com/ru/post/1689897/


All Articles