RegEx 返回找到的组 IF 字符串不以 ' 开头 - VSCode - 搜索/替换 [英] RegEx Return found Group IF String Does NOT start with ' - VSCode - Search/Replace

查看:47
本文介绍了RegEx 返回找到的组 IF 字符串不以 ' 开头 - VSCode - 搜索/替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我进行了全局可撤销的 RegEx 搜索和替换.我忘了在替换中包含 ' .现在我需要对与以下匹配的字符串进行搜索.它不能以 ' 开头,并且必须有 |最后翻译.这些是 Angular 翻译键 - 它们可以在模板文件 (HTML) 中无处不在.它们总是以 {{, have | 开头翻译,并以 }} 结尾.现在关键是他们可能有间距或换行问题(可能性较小,但有机会).所以它可能是 {{_ _ textToKeepAdd'To _ _ |_ _ 翻译 _ _ }} _ _ 是空格或换行的可能性.

So, I did a global undoable RegEx search and replace. I forgot to include the ' in the replace. Now I need to do a search on strings that match the below. It must not start with a ' and will have | translate at the end. These are Angular translation keys - they can be all over in the template file (HTML). They always begin with {{, have | translate, and end with }}. Now the kicker is they could have spacing or line break issues (less likely but a chance). So it could be {{_ _ textToKeepAdd'To _ _ | _ _ translate _ _ }} The _ _ being spaces or a line break possibility.

要匹配的字符串(无开头 '):

Strings To Match (no beginning '):

anyText' | translate

<other text or tags>{{ anyText' | translate

{{  // line break
anyText' | translate

anyText'
 | translate // line break

不匹配的字符串:

'anyText' | translate

 <other text or tags>{{ 'anyText' | translate

'anyText'
 | translate

返回字符串格式:

'anyText' | translate

示例:

blahadskfjlksjdf' | translate = 'blahadskfjlksjdf' | translate

'SkipMe' | translate = not found for replacement bc it starts with a '.

And <other text or tags>{{ anyText' | translate =  <other text or tags>{{ 'anyText' | translate

这是我反对的代码 - '(?:\w+\.){1,3}(?=\w+'\s+\|\s+translate\b)

Here is the code that I biffed on - '(?:\w+\.){1,3}(?=\w+'\s+\|\s+translate\b)

我将需要一组捕获/返回替换.

I am going to need a group capturing/returning in the replace.

推荐答案

这应该可以解决问题:

替换 \{\{(?:\s|\n)*(?!(?:'|\s|\n))(.*')(?:\s|\n)*(\|(?:\s|\n)+translate)\b

with {{ '$1 $2

正则表达式 101 演示

说明:

  • \{\{ - 匹配两个左大括号

  • \{\{ - match two open braces

(?:\s|\n)* - 匹配任意数量的空白字符

(?:\s|\n)* - match any number of whitespace characters

(?!(?:'|\s|\n))(.*') - 捕获组 1;匹配任何非 ' 字符后跟单个 '

(?!(?:'|\s|\n))(.*') - capture group 1; match any continuous string of not ' characters followed by a single '

(?:\s|\n)* - 匹配任意数量的空白字符

(?:\s|\n)* - match any number of whitespace characters

(\|(?:\s|\n)+translate) - 捕获第 2 组;匹配 | 后跟至少一个或多个空白字符,然后是单词 translate.

(\|(?:\s|\n)+translate) - capture group 2; match | followed by at least one, or more, whitespace character and then the word translate.

\b - 匹配单词边界

这篇关于RegEx 返回找到的组 IF 字符串不以 ' 开头 - VSCode - 搜索/替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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