简体   繁体   English

正则表达式:提取一个十进制数字,其后为R中的模式

[英]Regex : extracting a decimal number preceded by a pattern in R

Not sure what I am doing wrong here. 不知道我在做什么错。 I have lines in a text file...the target lines look like this 我在文本文件中有行...目标行看起来像这样

  • Nsource.Inhibitor 3 81.63 27.21 1.84 0.008 Nsource.Inhibitor 3 81.63 27.21 1.84 0.008
  • Nsource.Inhibitor 3 90.31 17.21 0.84 <0.001 Nsource.Inhibitor 3 90.31 17.21 0.84 <0.001

I want to extract the 0.008 and <0.001 from the end. 我想从末尾提取0.008和<0.001。

However, there are other lines that mean we have to use the first part of the line as part of a pattern.... 但是,还有其他几行表示我们必须将该行的第一部分用作模式的一部分。

  • Nsource 1 1238.10 1238.10 40.29 <.001 Nsource 1 1238.10 1238.10 40.29 <.001
  • Inhibitor 3 1484.41 494.80 16.10 <.001 抑制剂3 1484.41 494.80 16.10 <.001

My attempt 我的尝试

reline <- "+ Nsource.Inhibitor   3   81.63   27.21   1.84    0.008"
decnum <- "[[:digit:]]+\\.*[[:digit:]]*"
chk <- paste0("+ Nsource.Inhibitor[:blank:]+", decnum, "[:blank:]+", decnum, "[:blank:]+", decnum, "[:blank:]+", decnum,
       "[:blank:]+", "([[:digit:]]+\\.*[[:digit:]]*)")
gsub(chk, "\\1",reline)

returns: 返回:

"+ Nsource.Inhibitor\\t 3\\t 81.63\\t 27.21\\t 1.84\\t 0.008" “ + Nsource.Inhibitor \\ t 3 \\ t 81.63 \\ t 27.21 \\ t 1.84 \\ t 0.008”

Thanks for your help. 谢谢你的帮助。

Matt 马特

Something like this? 像这样吗

library(stringr)
strings <- c("Nsource.Inhibitor 3 81.63 27.21 1.84 0.008", "Nsource.Inhibitor 3 90.31 17.21 0.84 <0.001", 
             "Nsource 1 1238.10 1238.10 40.29 <.001", "Inhibitor 3 1484.41 494.80 16.10 <.001")

str_match(strings, "(?=^Nsource.Inhibitor).*?(<?\\d+\\.\\d+)$")[,2]

This yields 这产生

[1] "0.008"  "<0.001" NA       NA      

It ensures, there's Nsource.Inhibitor at the start of the string and only then matches the last \\d+.\\d+ pattern of that line (plus < eventually). 它确保在字符串的开头有Nsource.Inhibitor ,然后才匹配该行的最后\\d+.\\d+模式(最终加上< 。)。

If your target lines contain "Nsource.Inhibitor" and the last character is a number, and you want to extract all the characters after the last space, then try: 如果目标行包含“ Nsource.Inhibitor”,并且最后一个字符是数字,并且您要提取最后一个空格之后的所有字符,请尝试:

gsub(".*Nsource\\.Inhibitor.*\\s(.*[0-9])$", "\\1", reline)

You could add ignore.case = T if Nsource or Inhibitor appear without caps. 如果NsourceInhibitor没有大写字母,则可以添加ignore.case = T

Examples: 例子:

> reline <- "+ Nsource.Inhibitor   3   81.63   27.21   1.84    <0.008"
> output <- gsub(".*Nsource\\.Inhibitor.*\\s(.*[0-9])$", "\\1", reline, ignore.case = T)
> output
[1] "<0.008"

> reline <- "+ Nsource.Inhibitor   3   81.11  27  1232   23  123111  55.5555  0.38"
> output <- gsub(".*Nsource\\.inhibitor.*\\s(.*[0-9])$", "\\1", reline, ignore.case = T)
> output
[1] "0.38"
strings <- c("Nsource.Inhibitor 3 81.63 27.21 1.84 0.008", "Nsource.Inhibitor 3 90.31 17.21 0.84 <0.001",  "Nsource 1 1238.10 1238.10 40.29 <.001", "Inhibitor 3 1484.41 494.80 16.10 <.001")

下面的表达式使用grep拾取包含子字符串'Nsource.Inhibitor'的字符串,用' '分割字符串,并返回每个分割后的字符串的第6个元素。

sapply(strsplit(strings[grep('Nsource.Inhibitor', strings)], ' '), '[[',6)

There is no reason for using regex here. 这里没有理由使用正则表达式。 Simply read the file as a data.frame and do simple subsetting: 只需将文件读取为data.frame并进行简单的子设置即可:

DF <- read.table(text = "Nsource.Inhibitor 3 81.63 27.21 1.84 0.008
           Nsource.Inhibitor 3 90.31 17.21 0.84 <0.001
           Nsource 1 1238.10 1238.10 40.29 <.001
           nhibitor 3 1484.41 494.80 16.10 <.001", stringsAsFactors = FALSE) #you can read from file directly

DF[DF$V1 == "Nsource.Inhibitor", ncol(DF)]
#[1] "0.008"  "<0.001"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM