Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex, multiline extract in R

Tags:

regex

multiline

r

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!

like image 846
jim mako Avatar asked Mar 16 '23 02:03

jim mako


1 Answers

Try using the inline (?s) modifier which forces the dot . to span across newline sequences.

sub('(?s)</table>.*', '', x, perl = TRUE)
like image 174
hwnd Avatar answered Mar 23 '23 08:03

hwnd