非英语字符的正则表达式

时间:2016-10-25 11:01:28

标签: r regex special-characters

我需要检查一些字符串是否包含任何非英文字符。

x = c('Kält', 'normal', 'normal with, punctuation ~-+!', 'normal with number 1234')
grep(pattern = ??, x) # Expected output:1

1 个答案:

答案 0 :(得分:5)

您可以使用[^[:ascii:]] PCRE正则表达式:

x = c('Kält', 'normal', 'normal with, punctuation ~-+!', 'normal with number 1234')
grep(pattern = "[^[:ascii:]]", x, perl=TRUE) 
grep(pattern = "[^[:ascii:]]", x, value=TRUE, perl=TRUE) 

输出继电器:

[1] 1
[1] "Kält"

请参阅R demo

相关问题