简体   繁体   English

如何使用 R 更改字符串中文本之前的值

[英]How to change values before text in string using R

I have multiple strings that are similar to the following pattern:我有多个类似于以下模式的字符串:

dat<-("00000000AAAAAAAAAA0AAAAAAAAAA0AAAAAAAAAAAAAAAAAAAAAAAAD0")

I need to change all 0 values to "."我需要将所有 0 值更改为“。” before the first character value within a string.在字符串中的第一个字符值之前。 My desired output in this example would be:在此示例中,我想要的 output 将是:

"........AAAAAAAAAA0AAAAAAAAAA0AAAAAAAAAAAAAAAAAAAAAAAAD0". “........AAAAAAAAAA0AAAAAAAAAA0AAAAAAAAAAAAAAAAAAAAAAAAD0”。

I tried using gsub to accomplish this task:我尝试使用 gsub 来完成此任务:

gsub("\\G([^_\\d]*)\\d", ".\\1", dat, perl=T)

Unfortunately it changed all of the 0s to "."不幸的是,它把所有的 0 都改成了“.”。 instead of the 0s preceding the first "A".而不是第一个“A”之前的 0。

Can someone please help me with this issue?有人可以帮我解决这个问题吗?

If you wish to simply replace each leading 0 with a .如果您想简单地将每个前导0替换为. , you can use , 您可以使用

gsub("\\G0", ".", dat, perl=TRUE)

Here, \G0 matches a 0 char at the start of string, and then every time after a successful match.在这里, \G0匹配字符串开头的0字符,然后每次匹配成功后。 See this regex demo .请参阅此正则表达式演示

If you need to replace each 0 in a string before the first letter you can use如果您需要在第一个字母之前替换字符串中的每个0 ,您可以使用

gsub("\\G[^\\p{L}0]*\\K0", ".", dat, perl=TRUE)

Here, \G matches start of string or end of the preceding successful match, [^\p{L}0]* matches zero or more chars other than a letter and 0 , then \K omits the matched text, and then 0 matches the 0 char and it is replaced with a .这里, \G匹配字符串的开头或前面成功匹配的结尾, [^\p{L}0]*匹配零个或多个字符而不是字母和0 ,然后\K省略匹配的文本,然后0匹配0字符,它被替换为. . . See this regex demo .请参阅此正则表达式演示

See the R demo online :在线查看 R 演示

dat <- c("00000000AAAAAAAAAA0AAAAAAAAAA0AAAAAAAAAAAAAAAAAAAAAAAAD0","102030405000AZD")
gsub("\\G0", ".", dat, perl=TRUE)
## [1] "........AAAAAAAAAA0AAAAAAAAAA0AAAAAAAAAAAAAAAAAAAAAAAAD0"
## [2] "102030405000AZD"                                         
gsub("\\G[^\\p{L}0]*\\K0", ".", dat, perl=TRUE)
## [1] "........AAAAAAAAAA0AAAAAAAAAA0AAAAAAAAAAAAAAAAAAAAAAAAD0"
## [2] "1.2.3.4.5...AZD"                                         

This is really hard.这真的很难。 So I tried to do it with a custom function:所以我尝试使用自定义 function 来做到这一点:

library(stringr)

dat<-("00000000AAAAAAAAAA0AAAAAAAAAA0AAAAAAAAAAAAAAAAAAAAAAAAD0")

Zero_Replacer <- function(x) {
  x <- str_split(x, '[A-Za-z]', 2)
  x[[1]][1] <- str_replace_all(x[[1]][1], "0", ".")
  paste0(x[[1]][1], x[[1]][2])
}

Zero_Replacer(dat)

Output: Output:

[1] "........AAAAAAAAA0AAAAAAAAAA0AAAAAAAAAAAAAAAAAAAAAAAAD0"

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

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