Reputation: 1747
I am flatting an Angular translation file and I need to do a search replace in VScode on the translate key. I need to clip off all that comes before the final key and return that to the replace. The keys will only be at most 3 levels deep. The words below of key and keepThisInReplace
are arbitrary and will be different words. Examples below.
Search String Options:
'key1.keepThisInReplace1' | translate
'key1.key2.keepThisInReplace2' | translate
'key1.key2.key3.keepThisInReplace3' | translate
'anyWord.AndLevelAmount.UpTo3AtLeast.anyWordToKeep' | translate
Do not find in search:
'../../assets/images/
'./../assets/images/
'.......
'path').join
'staging.site
etc...
The above should be replaced as:
'keepThisInReplace1' | translate
'keepThisInReplace2' | translate
'keepThisInReplace3' | translate
'anyWordToKeep' | translate
What I am trying that is not working - Looks like '\w[^.].\w*.*\w*. will work. will almost work - picks up on 'path').join and 'staging.site still.
The keys above all do have {{
in front of them. But if I lock in on the {{ then I have to return it in the replace. Example = {{ key1.key2.keeptext | translate }}
= {{ keeptext | translate }}
Upvotes: 1
Views: 395
Reputation: 163642
You might use a non capturing group with a quantifier that repeats 1 - 3 times 1+ word characters followed by a dot and make sure multi line is enabled using the anchors ^$
.
In the replacement use a '
^'(?:\w+\.){1,3}(?=\w+'\s+\|\s+translate$)
If the string can be anywhere in the file your could omit the anchors and use a word boundary \b
after translate.
'(?:\w+\.){1,3}(?=\w+'\s+\|\s+translate\b)
Explanation
^
Start of string(?:\w+\.){1,3}
Repeat 1 - 3 times matching 1+ word chars and a dot(?=
Positive lookahead, assert what is directly on the right is
\w+'\s+
Match 1+ word chars, '
and 1+ whitespace chars\|
Match |
\s+translate$
Match 1+ whitespace chars, translate
and assert end of the string)
Close positive lookaheadAnother option instead of a positive lookahead is using 2 capturing groups.
In the replacement use group 1 and group 2 $1$2
^(')(?:\w+\.){1,3}(\w+'\s+\|\s+translate$)
Upvotes: 1
Reputation: 1927
'.*\.
to '
should be all you need. This will replace all strings that match a tick followed by any number of characters and then a dot.
if you want to be more specific you can use this
'\w*\.*\w*\.*\w*\.
this will match up to 3 dots with words in between.
Check the demo here: https://regexr.com/4g652
EDIT
I tried to make it more specific and here is what I came up with:
'.+\.(?=\w*'\s*\|\s*translate)
This uses a lookahead to make sure the matched string is followed by a word and then ' | translate
. Do you think this is specific enough?
Demo: https://regexr.com/4g66i
Upvotes: 0