我正在清理一个大的非结构化数据--实际上是一个代码页。我需要从这段代码中提取一个特定的短语,从
以下是.txt文件中的示例数据。我需要查找
</bi:data_source_alias>
<bi:component name="ROOT" type="ABSOLUTE_LAYOUT_COMPONENT">
<bi:component name="CHART_1" type="com_sap_ip_bi_VizFrame">
<bi:property name="LEFT_MARGIN" value="31"/>` <bi:property name="TOP_MARGIN" value="64"/>
<bi:component name="SCORECARD_1" type="com_sap_ip_bi_Scorecard">
<bi:property name="LEFT_MARGIN" value="9"/>
这就是我到目前为止所做的。
发布于 2020-10-17 11:11:17
如果您想要将包含type描述的新列type添加到包含post中给出的大字符串的现有dataframe df中,则应该可以:
解决方案:
df$component <- str_extract_all(df$v1, '(?<=bi:component name="\\w{1,100}" type=")[^"]+')这利用了(?<=...)的正向查找,以及\\w不仅匹配字母,还匹配数字和下划线的事实,这些都与bi:component name的值有关。
结果:
df
v1
1 </bi:data_source_alias>\n <bi:component name="ROOT" type="ABSOLUTE_LAYOUT_COMPONENT">\n <bi:component name="CHART_1" type="com_sap_ip_bi_VizFrame">\n <bi:property name="LEFT_MARGIN" value="31"/>` <bi:property name="TOP_MARGIN" value="64"/>\n<bi:component name="SCORECARD_1" type="com_sap_ip_bi_Scorecard">\n <bi:property name="LEFT_MARGIN" value="9"/>
component
1 ABSOLUTE_LAYOUT_COMPONENT, com_sap_ip_bi_VizFrame, com_sap_ip_bi_Scorecard数据:
df <- data.frame(
v1 = '</bi:data_source_alias>
<bi:component name="ROOT" type="ABSOLUTE_LAYOUT_COMPONENT">
<bi:component name="CHART_1" type="com_sap_ip_bi_VizFrame">
<bi:property name="LEFT_MARGIN" value="31"/>` <bi:property name="TOP_MARGIN" value="64"/>
<bi:component name="SCORECARD_1" type="com_sap_ip_bi_Scorecard">
<bi:property name="LEFT_MARGIN" value="9"/>'
)https://stackoverflow.com/questions/64401020
复制相似问题