问题
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!
回答1:
Try using the inline (?s) modifier which forces the dot . to span across newline sequences.
sub('(?s)</table>.*', '', x, perl = TRUE)
来源:https://stackoverflow.com/questions/30144325/regex-multiline-extract-in-r