Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.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 Grep-in-R匹配获取非数字_Regex_R - Fatal编程技术网

Regex Grep-in-R匹配获取非数字

Regex Grep-in-R匹配获取非数字,regex,r,Regex,R,我需要得到字符的非数字部分。我对R中的这个正则表达式有问题(根据regexpal,它应该可以工作): 它应该返回“PC”,同时返回字符(0) 其他测试用例: grep("[\\D]+", "STON/O2. 3101282 ", value = TRUE, perl = F) # should return "STON/O2." grep("[\\D]+", "S.C./A.4. 23567", value = TRUE, perl = F) # should return "S.C./A

我需要得到字符的非数字部分。我对R中的这个正则表达式有问题(根据regexpal,它应该可以工作):

它应该返回
“PC”
,同时返回
字符(0)

其他测试用例:

grep("[\\D]+", "STON/O2. 3101282    ", value = TRUE, perl = F)
# should return "STON/O2."
grep("[\\D]+", "S.C./A.4. 23567", value = TRUE, perl = F)
# should return "S.C./A.4."
grep("[\\D]+", "C.A. 31026", value = TRUE, perl = F)
# should return "C.A."
更新

工作是将列
“Ticket”
(来自泰坦尼克号灾难数据库)分为
“TicketNumber”
“TicketSeries”
列。就目前而言,票证的价值如下:
“A/521171”、“PC 17599”、“STON/O2.3101282”、“113803”
。因此,票号列用于第一条记录
21171
,票号系列列
“A/5”
,依此类推用于下一条记录

对于记录
“113803”
,票号应为
“113803”
,票号应为
NA

感谢您的帮助,
谢谢

改用
sub
,使用
\S
regex标记匹配任何非空白字符

x <- c('PC 17610', 'STON/O2. 3101282    ', 'S.C./A.4. 23567', 'C.A. 31026')
sub('(\\S+).*', '\\1', x)
# [1] "PC"        "STON/O2."  "S.C./A.4." "C.A."

x改用
sub
,使用
\S
regex标记匹配任何非空白字符

x <- c('PC 17610', 'STON/O2. 3101282    ', 'S.C./A.4. 23567', 'C.A. 31026')
sub('(\\S+).*', '\\1', x)
# [1] "PC"        "STON/O2."  "S.C./A.4." "C.A."

x您可以使用
stru-extract

library(stringr)
str_extract(x, '\\S+(?=\\s+)')
#[1] "PC"        "STON/O2."  "S.C./A.4." "C.A."      NA       
数据
x您可以使用
stru-extract

library(stringr)
str_extract(x, '\\S+(?=\\s+)')
#[1] "PC"        "STON/O2."  "S.C./A.4." "C.A."      NA       
数据
x是否有使用
grep
的解决方案,或者你根本不在乎?我在乎,但我还没有……试试
library(stringr);str_extract(x,'\\S+(?=\\S+))#[1]“PC”“STON/O2”“S.C./A.4”“C.A.”NA
where
x谢谢
train$TicketSeries是否有使用
grep
的解决方案,或者你根本不在乎?我在乎,但我还没有……试试
library(stringr);str_extract(x,'\\S+(?=\\S+))#[1]“PC”“STON/O2”“S.C./A.4”“C.A.”NA
where
x谢谢<代码>火车$TicketSeries您可能知道如何返回sub(“(\\S+).*”、“\\1”、“31026”)的NA吗?PS:我更新了问题以更好地描述我的问题。您可能知道如何返回sub(“(\\S+).*”、“\\1”、“31026”)的NA吗?PS:我更新了问题,以便更好地描述我的问题
x <- c('PC 17610', 'STON/O2. 3101282    ', 'S.C./A.4. 23567', 
        'C.A. 31026', '31026')