gpt4 book ai didi

非英文字符的正则表达式

转载 作者:行者123 更新时间:2023-12-05 00:54:22 30 4
gpt4 key购买 nike

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

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

最佳答案

您可以使用 [^[: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

关于非英文字符的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40238217/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com