使用 R 正则表达式进行模式匹配的问题
Problems pattern matching using R regular expressions
我正在尝试使用 str_extract 提取字符串。下面是字符串类型的一个小例子:
library(stringr)
gs<-"{\"type\":\"Polygon\",\"coordinates\":[[[1,2],[3,4],[5,6],[7,8]]]}"
s='\{\\"type\\"*\}'
str_extract(gs,s)
我想要打印出整个字符串(真正的字符串会有更多这种类型的字符,并且应该只 return 我在此处指定的部分)。相反,我得到了 NA。对于我做错了什么的任何想法,我将不胜感激。谢谢!
这是你想要的吗?
gs<-"{\"type\":\"Polygon\",\"coordinates\":[[[1,2],[3,4],[5,6],[7,8]]]} I DO NOT WANT THIS {\"type\":\"Not a Polygon\",\"coordinates\":[[[1,2],[3,4],[5,6],[7,8]]]}"
s="\{\"type\"(.*?)\}"
result = str_match_all(gs,s)[[1]][,1]
为了测试,我添加了不应返回的字符串 'I DO NO WANT THIS',并添加了第二个类型为 'not a polygon'
的对象
它returns:
"{\"type\":\"Polygon\",\"coordinates\":[[[1,2],[3,4],[5,6],[7,8]]]}"
"{\"type\":\"Not a
Polygon\",\"coordinates\":[[[1,2],[3,4],[5,6],[7,8]]]}"
所以只有请求的元素。希望这对您有所帮助!
我正在尝试使用 str_extract 提取字符串。下面是字符串类型的一个小例子:
library(stringr)
gs<-"{\"type\":\"Polygon\",\"coordinates\":[[[1,2],[3,4],[5,6],[7,8]]]}"
s='\{\\"type\\"*\}'
str_extract(gs,s)
我想要打印出整个字符串(真正的字符串会有更多这种类型的字符,并且应该只 return 我在此处指定的部分)。相反,我得到了 NA。对于我做错了什么的任何想法,我将不胜感激。谢谢!
这是你想要的吗?
gs<-"{\"type\":\"Polygon\",\"coordinates\":[[[1,2],[3,4],[5,6],[7,8]]]} I DO NOT WANT THIS {\"type\":\"Not a Polygon\",\"coordinates\":[[[1,2],[3,4],[5,6],[7,8]]]}"
s="\{\"type\"(.*?)\}"
result = str_match_all(gs,s)[[1]][,1]
为了测试,我添加了不应返回的字符串 'I DO NO WANT THIS',并添加了第二个类型为 'not a polygon'
的对象它returns:
"{\"type\":\"Polygon\",\"coordinates\":[[[1,2],[3,4],[5,6],[7,8]]]}"
"{\"type\":\"Not a Polygon\",\"coordinates\":[[[1,2],[3,4],[5,6],[7,8]]]}"
所以只有请求的元素。希望这对您有所帮助!