![](/img/trans.png)
[英]Remove first occurrence of special characters until the first word or word character in R using regex
[英]Regex “until but not including” for special characters (using R)
我有一個像這樣的數據框(RNA.patients):
PAK1|5808
PAK2|10289
PALM2|114299
PALM2-AKAP2|445815
我想抓住所有東西直到“|”,所以我找到了這個正則表達式:
regmatches(RNA.patients[i,1], regexpr("^[^[:punct:]]*", RNA.patients[i,1]))
但對於像“PALM2-AKAP2”這樣的情況,正則表達式停在“ - ”處。 有人能幫助我嗎?
而不是使用正則表達式(除非它們確實是必要的),只需使用列上的read.table
將其拆分為兩個並從那里提取相關列:
Text <- c("PAK1|5808", "PAK2|10289", "PALM2|114299", "PALM2-AKAP2|445815")
read.table(text = Text, sep = "|")
# V1 V2
# 1 PAK1 5808
# 2 PAK2 10289
# 3 PALM2 114299
# 4 PALM2-AKAP2 445815
或者,您可以使用strsplit
:
sapply(strsplit(Text, "[|]"), `[[`, 1)
# [1] "PAK1" "PAK2" "PALM2" "PALM2-AKAP2"
sapply(strsplit(Text, "[|]"), `[[`, 2)
# [1] "5808" "10289" "114299" "445815"
我想你可以將否定移動到punct子類,然后添加 - 到外部類
^[-[:^punct:]]*
如果你想全局匹配字段,請使用它
[-[:^punct:]]+
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.