正则表达式查找以特定字符串开头的行中的所有空格

Regex to find all spaces in lines beginning with a specific string

我正在搜索一个正则表达式来查找以特定字符串开头的行中的所有 space(在 SVN 转储文件中)。尽管 "global" 修饰符我的正则表达式 returns 仅第一次出现 space 字符。

我正在处理的文件的一部分:

...
pla bla bli

Node-path: branches/BU ILD/ml_cf/syst em/Translation/TranslationManager.class.php  
Node-kind: file
Node-action: change
Text-delta: true
....

正则表达式:

/Node-path: \S*(\ )/g

只找到第一个 space(在 U 和 I 之间),而不是线上的其他。

使用 PCRE 正则表达式查找以特定文本开头的行中的所有 space,请使用此正则表达式:

/(?:^Node-path: |\G)\S+\K\h+/gm

RegEx Demo

  • 使用 (?:Node-path: |\G) 我们匹配以 Node-path: 开头的行或定位在上一个匹配项的末尾。
  • \G 断言位置在前一个匹配的末尾或第一个匹配的字符串的开头
  • \K 重置报告比赛的起点。
  • \h+ 匹配 1 个或多个水平白色space(space 或制表符)