如何识别和提取R中的字母数字字符 [英] How to recognise and extract alpha numeric characters in R

查看:1636
本文介绍了如何识别和提取R中的字母数字字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

我想从R.中的分句中提取字母数字字符
我试过以下内容: aa = grep([:alnum:],abc)

code> integer(0),但它返回1,不应该是这种情况,因为abc不是字母数字。
我在这里错过了什么?
基本上我在寻找一个函数,它只搜索字母和数字组合的字符,例如:ABC-0112,PCS12SCH
预先感谢您的帮助。 p>

解决方案

[[:alnum:]] 匹配字母或数字。为了匹配包含两者的字符串,您应该使用

  x < -  c(ABC,ABc12 , -  A-1,abc--,89 = A)
grep((。* [[:alpha:]。* [[:digit:]] |。* [[:digit:]]。* [[:alpha:]]),x)
#[1] 2 3 5

  which(grepl([[:alpha:]], x)& grepl([[:digit:]],x))
#[1] 2 3 5


I want to extract alphanumeric characters from a partiular sentence in R. I have tried the following:

aa=grep("[:alnum:]","abc")

.This should return integer(0),but it returns 1,which should not be the case as "abc" is not an alphanumeric. What am I missing here? Essentially I am looking for a function,that only searches for characters that are combinations of both alphabets and numbers,example:"ABC-0112","PCS12SCH" Thanks in advance for your help.

解决方案

[[:alnum:]] matches alphabets or digits. To match the string which contains the both then you should use,

x <- c("ABC", "ABc12", "--A-1", "abc--", "89=A")
grep("(.*[[:alpha:]].*[[:digit:]]|.*[[:digit:]].*[[:alpha:]])", x)
# [1] 2 3 5

or

which(grepl("[[:alpha:]]", x) & grepl("[[:digit:]]", x))
# [1] 2 3 5   

这篇关于如何识别和提取R中的字母数字字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆