I am having some problems with deleting everything after the first occurrence of a pattern in R. I have imported the data with paste(readLines(url), collapse="\n")
.
For example, my string is, \"id=\"fruit_info\">\n<tr class='thead'>\n<th colspan=2>Strawberries</th></table>\n</tr>\n</table>\n<tr class
.
I want to remove everything after the first occurrence of </table>
. What I want to see is;
\"id=\"fruit_info\">\n<tr class='thead'>\n<th colspan=2>Strawberries</th>
The methods I am trying do not seem to register the first </table>
occurrence and not providing the intended results.
Thanks!
Try using the inline (?s)
modifier which forces the dot .
to span across newline sequences.
sub('(?s)</table>.*', '', x, perl = TRUE)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With