Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有一种方法可以在R脚本中使用正则表达式从两个字符之间提取数据,并考虑不一致的间距?_R_Regex - Fatal编程技术网

有没有一种方法可以在R脚本中使用正则表达式从两个字符之间提取数据,并考虑不一致的间距?

有没有一种方法可以在R脚本中使用正则表达式从两个字符之间提取数据,并考虑不一致的间距?,r,regex,R,Regex,我目前正在尝试使用str_match函数从pdf中提取数据,该函数运行良好。这是一个例子: values[[18]] <- str_match(Sprout_textNoLines, "Business Description: (.*?) Renter or Owned:")[,2] 我之前展示的str_match调用返回的是“联邦和州建设”,这正是我所需要的。但是,我发现有些情况下,某些PDF是不同的,并且线路上的输入不会被空格分隔,例如: Business Desc

我目前正在尝试使用str_match函数从pdf中提取数据,该函数运行良好。这是一个例子:

    values[[18]] <- str_match(Sprout_textNoLines, "Business Description: (.*?) Renter or Owned:")[,2]
我之前展示的str_match调用返回的是“联邦和州建设”,这正是我所需要的。但是,我发现有些情况下,某些PDF是不同的,并且线路上的输入不会被空格分隔,例如:

    Business Description:Federal and State Construction Renter or Owned:
此处的Description:和Federal之间没有空格,因此前面的函数调用只会在此处拉回NA,因为业务描述:(.*)Renter或Owned:。我需要自动化这个过程,所以是否有一个正则表达式可以实现类似的功能

    values[[18]] <- str_match(Sprout_textNoLines, "Business Description: (.*?) Renter or Owned:")[,2] 
值[[18]]您可以使用

str_match(Sprout_textNoLines, "Business Description:\\s*(.*?)\\s*Renter or Owned:")[,2]


更改的部分是匹配0个或多个空格(
\s*
)的
\s*(.*)\s*
),然后尽可能少地捕获除换行符以外的任何0个或多个字符,然后再次匹配0个或多个空格。

您能否显示可复制的示例
str\u match(spreat\u textNoLines,“业务描述:\\s*(.*)承租人或所有人:”)[,2]
\\s*
表示零个或多个空间。
str_match(Sprout_textNoLines, "Business Description:\\s*(.*?)\\s*Renter or Owned:")[,2]