问题描述
在R中第一次出现模式后,删除所有内容时遇到一些问题.我已经用paste(readLines(url), collapse="\n")
导入了数据.
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")
.
例如,我的字符串是\"id=\"fruit_info\">\n<tr class='thead'>\n<th colspan=2>Strawberries</th></table>\n</tr>\n</table>\n<tr class
.
我想在第一次出现</table>
之后删除所有内容.我想看的是
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>
我尝试的方法似乎没有注册第一个</table>
事件,并且未提供预期的结果.
The methods I am trying do not seem to register the first </table>
occurrence and not providing the intended results.
谢谢!
推荐答案
尝试使用内联(?s)
修饰符,该修饰符会强制点.
跨越换行符序列.
Try using the inline (?s)
modifier which forces the dot .
to span across newline sequences.
sub('(?s)</table>.*', '', x, perl = TRUE)
这篇关于正则表达式,R中的多行提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!