RegEx Return found Group IF String Does NOT start with ' - VSCode - Search/Replace

橙三吉。 提交于 2019-12-25 03:16:18

问题


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

Strings Not To Match:

'anyText' | translate

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

'anyText'
 | translate

Return String Format:

'anyText' | translate

Example:

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

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.


回答1:


This should do the trick:

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

with {{ '$1 $2

Regex 101 Demo

Explanation:

  • \{\{ - match two open braces

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

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

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

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

  • \b - match a word boundary




回答2:


I suggest using

Find What:      \{\{[\s\n]*(?!['\s\n])(.*')[\s\n]*(\|[\s\n]+translate)\b
Replace With: {{ '$1 $2

See online regex demo (altered to reflect how it works in VSCode).

Details

  • ^ - start of a line
  • \{\{ - a {{ substring
  • [\s\n]* - 0+ whitespaces/linebreaks
  • (?!['\s\n]) - a negative lookahead failing the match if immediately to the right of the current location there is a ' or a whitespace (linebreak included)
  • (.*') - Capturing group 1: any 0+ chars other than line break chars, as many as possible and then a ' char
  • [\s\n]* - 0+ whitespaces/linebreaks
  • (\|[\s\n]+translate)\b - Group 2: a |, 1+ whitespaces/linebreaks and a whole word translate.

The replacement is ', Group 1 backreference (referring to the value captured in Group 1), space and Group 2 backreference (referring to the value captured in Group 2).



来源:https://stackoverflow.com/questions/56707400/regex-return-found-group-if-string-does-not-start-with-vscode-search-repla

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!