regex - 正则表达式匹配和替换 pvalue 字符串

标签 regex r

希望这有意义。

我有一串不同长度的p值(由于四舍五入),其中非常大和小的p值存储为1.0(some number of 0s corresponding to the length of rounding) ,和0.0(some number of 0s corresponding to the length of rounding) , 分别。

我想匹配两组模式:

第一: "(1.)(string of zeros of any length)" 并将其更改为 "> 0.(sting of nines the same length as the string of zeros)"

第二 "(0.)(string of zeros of any length)" 并将其更改为 "< 0.(string of zeros the length of the input minus one)1 .

因此,如果我们有以下输入:

pvals<-c("1.000","1.00","0.00000","0.123","0.6","0.0")

我希望返回:

> expectedOutput
[1] "> 0.999"   "> 0.99"    "< 0.00001" "0.123"     "0.6"       "< 0.1" 

我一直在尝试使用gsub,但我对正则表达式的更复杂使用知之甚少,我不明白如何允许某个字符(0)的任意长度,然后如何替换为相同数量的新字符(在 1.0s 的情况下),或该数字减 1(在 0.0s 的情况下)

任何帮助将不胜感激! 谢谢

最佳答案

你可以做这样的事情

> pvals<-c("1.000","1.00","0.00000","0.123","0.6","0.0")
> x <- gsub("(?:^1\\.|\\G)\\K0(?=0*$)", "9", pvals, perl=T)
> m <- gsub("^1\\.", "> 0.", x)
> gsub("^(0\\.0*)0$", "< \\11", m)
[1] "> 0.999"   "> 0.99"    "< 0.00001" "0.123"    
[5] "0.6"       "< 0.1"   

关于regex - 正则表达式匹配和替换 pvalue 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29344262/

相关文章:

Java 使用正则表达式匹配器获取组

java - 使用正则表达式检查数字范围

r - 将某些列的名称作为数据框每一行的一个单元格

r - 对两列以上使用 igraph

r - ggplot2 中的语音注视事件图

R:如何在集团层面重新采样日内数据?

ruby - 用换行符替换管道字符 "|"?

PHP:字符串到多维数组

javascript - 正则表达式用另一个字符替换每个字符,除了方括号中的内容

r - 是否有 R 函数来获取预期计数表?