Reputation: 69
I used readLines to import a text file and I collapsed all the lines. Now I want to write a function that would cycle through the entire collapsed text and detect the end of each sentence starting a new line for each sentence. It would detect (periods, question marks, periods followed by quotations, or question marks followed by quotations)
so for example:
"I need help. How do I write this code?"
would become:
I need help.
How do I write this code?
Does anyone know how I might go about accomplishing this?
Upvotes: 0
Views: 54
Reputation: 611
gsub might work.
gsub('. ', '.\n', your_text)
replaces the '. '
pattern by '\n '
which is the symbol for a line break.
your_text = 'lets. try'
aa = gsub('. ', '.\n', your_text)
print(aa)
cat(aa)
Upvotes: 1
Reputation: 388982
We can use positive look behind regex to match for a "."
or a question mark "?"
and replace it with new line (\n
).
str = "I need help. How do I write this code? "
cat(gsub('(?<=[.?])\\s', '\n', str, perl = TRUE))
#I need help.
#How do I write this code?
Upvotes: 0