Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.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
Regex 如何确定R中的字符串是否包含非罗马字符_Regex_R_String - Fatal编程技术网

Regex 如何确定R中的字符串是否包含非罗马字符

Regex 如何确定R中的字符串是否包含非罗马字符,regex,r,string,Regex,R,String,确定字符串是否包含非罗马/非英语(例如,ないでさ)字符?您可以使用regex/grep检查可打印ASCII字符范围之外的字符的十六进制值: x <- 'ないでさ' grep( "[^\x20-\x7F]",x ) #[1] 1 grep( "[^\x20-\x7F]","Normal text" ) #integer(0) Henrik Bengtsson认为在他的软件包中包含这些内容是合适的,这一事实告诉我,在base/default R中没有一种简便的方法可以做到这一点。他是一个长期

确定字符串是否包含非罗马/非英语(例如,ないでさ)字符?

您可以使用regex/grep检查可打印ASCII字符范围之外的字符的十六进制值:

x <- 'ないでさ'
grep( "[^\x20-\x7F]",x )
#[1] 1
grep( "[^\x20-\x7F]","Normal text" )
#integer(0)
Henrik Bengtsson认为在他的软件包中包含这些内容是合适的,这一事实告诉我,在base/default R中没有一种简便的方法可以做到这一点。他是一个长期用户/专家

看到另一个答案,这一努力似乎很直接:

> is.na( iconv( c(x, "OrdinaryASCII") , "", "ASCII") )
[1]  TRUE FALSE

您可以确定字符串是否包含带有
iconv
grep
的非拉丁/非ASCII字符

# My example, because you didn't add your data
characters <- c("ないでさ,  satisfação, катынь, Work, Awareness, Potential, für")
# First you convert string to vector of words
characters.unlist <- unlist(strsplit(characters, split=", "))
# Then find indices of words with non-ASCII characters using ICONV
characters.non.ASCII <- grep("characters.unlist", iconv(characters.unlist, "latin1", "ASCII", sub="characters.unlist"))
# subset original vector of words to exclude words with non-ASCII characters
data <- characters.unlist[-characters.non.ASCII]
# convert vector back to a string
dat.1 <- paste(data, collapse = ", ")

# Now if you run 
characters.non.ASCII
[1] 1 2 3 7 

你的问题不符合网站指南。重新措辞,让你询问如何做某事,更重要的是,展示你迄今为止的尝试。就目前而言,这个问题相当广泛,而且是“寻找工具”。我不理解反对票。这似乎是一个完全合理的问题,而且在搜索时无法立即找到。我总是很难找到char to integer函数。我永远记不起是char2Int,ChartPoint,chr2Int还是其他什么。。。因为它不是none或者那些.0x7F可能是不可打印的。它不取决于我们正在讨论的输出设备吗?TTY可能会退格。R控制台从
iconv(“\x7f”,“ASCII”)
返回“\177”(八进制),这与从
R.oo::intToChar(0x7f)
返回的结果相同。我认为这是DEL控制代码,但确保任何控制代码都会对输出设备产生影响。7位ASCII为0x00-0x7F
# My example, because you didn't add your data
characters <- c("ないでさ,  satisfação, катынь, Work, Awareness, Potential, für")
# First you convert string to vector of words
characters.unlist <- unlist(strsplit(characters, split=", "))
# Then find indices of words with non-ASCII characters using ICONV
characters.non.ASCII <- grep("characters.unlist", iconv(characters.unlist, "latin1", "ASCII", sub="characters.unlist"))
# subset original vector of words to exclude words with non-ASCII characters
data <- characters.unlist[-characters.non.ASCII]
# convert vector back to a string
dat.1 <- paste(data, collapse = ", ")

# Now if you run 
characters.non.ASCII
[1] 1 2 3 7 
dat.1 #and the output will be all ASCII charaters
[1] "Work, Awareness, Potential"