不区分大小写匹配正则表达式,替换为特定大小写 [英] Match regular expression case insensitively, replace with specific case

查看:85
本文介绍了不区分大小写匹配正则表达式,替换为特定大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用正则表达式来替换一些子字符串.替换值重用部分匹配.我想不区分大小写地匹配,但在替换中,我想要匹配的东西的小写版本.

I'm using regular expressions to replace some substrings. The replacement value reuses part of the match. I want to match case insensitively, but in the replacement, I want a lower case version of the thing that was matched.

library(stringi)
x <- "CatCATdog"
rx <- "(?i)(cat)(?-i)"
stri_replace_all_regex(x, rx, "{$1}")
# [1] "{Cat}{CAT}dog"

这与我想要的很接近,除了cat"应该是小写.也就是说,输出字符串应该是 "{cat}{cat}dog".

This is close to what I want, except the "cat"s should be lower case. That is, the output string should be "{cat}{cat}dog".

以下代码不起作用,但它显示了我的意图.

The following code doesn't work, but it shows my intension.

stri_replace_all_regex(x, rx, "{tolower($1)}") 

以下技术确实有效,但它很丑陋,不是很通用,也不是很有效.我的想法是用与我想要的匹配的正则表达式替换正则表达式,而不是替换值(即cat"而不是{cat}").然后在每个输入字符串中搜索第一个匹配项,找到匹配项的位置,做一个子串替换,然后寻找下一个匹配项,直到没有匹配项为止.太恶心了.

The following technique does work, but it's ugly, not very generalizable, and not very efficient. My idea was to replace the regular expression with one that matches what I want, but not the replacement values (that is, "cat" but not "{cat}"). Then search for the first match in each input string, find the location of the match, do a substring replacement, then look for the next match until there are no more. It's awful.

x <- "CatCATdog"
rx <- "(?i)((?<!\\{)cat(?!\\}))(?-i)"
repeat{
  detected <- stri_detect_regex(x, rx)
  if(!any(detected))
  {
    break
  }
  index <- stri_locate_first_regex(x[detected], rx)
  match <- tolower(stri_match_first_regex(x[detected], rx)[, 2])
  stri_sub(x[detected], index[, 1], index[, 2]) <- paste0("{", match[detected], "}")
}

我觉得一定有更好的方法.

I feel like there must be a better way.

如何用小写值替换不区分大小写的匹配项?

How do I replace case insensitive matches with lower case values?

多亏评论的启发,我发现我要找的东西是替换文本大小写转换".

Thanks to inspiration from the comments, I discovered that the thing I'm looking for is "replacement text case conversion".

推荐答案

可以使用\\L将匹配的大小写改为小写

You can use \\L to change the case of the match to lower

gsub(rx, "{\\L\\1}", x, perl=TRUE) 

这篇关于不区分大小写匹配正则表达式,替换为特定大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆