首页 > 解决方案 > 在字符串中查找多个空格

问题描述

我试图在以“jpeg”开头并以 600 结尾定义的字符串中查找所有空格),以便用“_”替换它们,但是如何捕获字符串中的所有 \s?

我正在使用崇高的文本编辑器/记事本++

我试过了:

^jpeg.*(\s).*600\)$

感谢您的帮助正在编辑的文本示例:

# CHART: Share of persons living at risk of poverty or social exclusion ====
df <- S3R0004_M3080242 %>% 
        mutate(LAIKOTARPIS=parse_date_time(LAIKOTARPIS, "y")) 
jpeg("./figures/Share of persons living at risk of poverty or social exclusion.jpeg", width = 9, height = 6, units = 'in', res = 600)
ggplot(data = df, aes(x=LAIKOTARPIS, y=obsValue)+

标签: regexsublimetext3

解决方案


您可以使用

查找内容:      (?:\G(?!\A)|jpeg\("(?=[^"]*"[^)]*600\)))[^\s"]*\K\s+
替换为_

请参阅正则表达式演示

细节

  • (?:\G(?!\A)|jpeg\("(?=[^"]*"[^)]*600\)))- 匹配任一
    • \G(?!\A)- 上一场比赛的结束
    • |- 或者
    • jpeg\("(?=[^"]*"[^)]*600\))-jpeg("后跟除"(with [^"]*) 之外的任何 0+ 字符,然后是除and"之外的任何 0+ 字符)600)
  • [^\s"]*- 匹配并消耗 0+ 字符而不是空格和"
  • \K- 匹配重置运算符,之前匹配的文本从匹配缓冲区中清除
  • \s+- 1+ 个空格(它们将被替换)。

推荐阅读