{{dockerhub | d('test')}}/{{kafka_topic_exporter_repository|default('kafka-topic-exporter') }}
Here I want 2 mathes {{ .* }} But regex {{\s*\(\S\+.*\S\+\)\s*}} matches the entire line. Is there any way to do so ?
Vim supports non-greedy matching with the \{-} operator:
{{\zs.\{-}\ze}}
this will match text inside the first braces: dockerhub | d('test').
\zs starts the match after {{.
\ze ends the match before }}.
If you need a capturing group replace .\{-} with \(.\{-}\).
Related
in nodejs, I have to following pattern
(/^(>|<|>=|<=|!=|%)?[a-z0-9 ]+?(%)*$/i
to match only alphanumeric strings, with optional suffix and prefix with some special characters. And its working just fine.
Now I want to match the last '%' only if the first character is alphanumeric (case insensitive) or also a %. Then its optionally allowed, otherwise it should not match.
Example:
Should match:
>test
!=test
<test
>=test
<=test
%test
test
%test%
test%
Example which should not match:
<test% <-- its now matching, which is not correct
<test< <-- its now **not** matching, which is correct
Any Ideas?
You can add a negative lookahead after ^ like
/^(?![^a-z\d%].*%$)(?:[><]=?|!=|%)?[a-z\d ]+%*$/i
^^^^^^^^^^^^^^^^^
See the regex demo. Details:
^ - start of string
(?![^a-z\d%].*%$) - fail the match if there is a char other than alphanumeric or % at the start and % at the end
(?:[><]=?|!=|%)? - optionally match <, >, <=, >=, != or %
[a-z\d ]+ - one or more alphanumeric or space chars
%* - zero or more % chars
$ - end of string
You might use an alternation | to match either one of the options.
^(?:[a-z0-9%](?:[a-z0-9 ]*%)?|(?:[<>]=?|!=|%)?[a-z0-9 ]+)$
^ Start of string
(?: Non capture group
[a-z0-9%] Match one of the listed in the character class
(?:[a-z0-9 ]*%)? Optionally match repeating 0+ times any of the character class followed by %
| Or
(?:[<>]=?|!=|%)? Optionally match one of the alternatives
[a-z0-9 ]+ Match 1+ times any of the character class
) Close non capture group
$ End of string
Regex demo
I need to remove both duplicates like:
admin
user
admin
result:
user
I have tried but none works for notepad++
You have to sort your file before apply this (for example using the plugin TexFX).
Ctrl+H
Find what: ^(.+)(?:\R\1)+
Replace with: NOTHING
check Wrap around
check Regular expression
DO NOT CHECK . matches newline
Replace all
Explanation:
^ : Beginning of line
(.+) : group 1, 1 or more any character but newline
(?: : start non capture group
\R : any kind of linebreak
\1 : content of group 1
)+ : end group, must appear 1 or more times
I need to clear text around other text... example:text = hello there sir how are you doing
Tex to be removed = hello sir are doing
Result = there how you
What would i put in to skip all other text but the words in the text to be removed with the ctrl h menu.
You may use a regex based search and replace:
Find what: \S+(?:\s+(\S+))?
Replace with: $1
See the regex demo
Details:
\S+ - one or more chars other than whitespace
(?:\s+(\S+))? - one or zero occurrences of
\s+ - 1+ whitespaces
(\S+) - Group 1: one or more chars other than whitespace
You can do find/replace with regexes in Notepad++.
I would like to use vim's substitute function (:%s) to search and replace a certain pattern of code. For example if I have code similar to the following:
if(!foo)
I would like to replace it with:
if(foo == NULL)
However, foo is just an example. The variable name can be anything.
This is what I came up with for my vim command:
:%s/if(!.*)/if(.* == NULL)/gc
It searches the statements correctly, but it tries to replace it with ".*" instead of the variable that's there (i.e "foo"). Is there a way to do what I am asking with vim?
If not, is there any other editor/tools I can use to help me with modifications like these?
Thanks in advance!
You need to use capture grouping and backreferencing in order to achieve that:
Pattern String sub. flags
|---------| |------------| |-|
:%s/if(!\(.*\))/if(\1 == NULL)/gc
|---| |--|
| ^
|________|
The matched string in pattern will be exactly repeated in string substitution
:help /\(
\(\) A pattern enclosed by escaped parentheses. /\(/\(\) /\)
E.g., "\(^a\)" matches 'a' at the start of a line.
E51 E54 E55 E872 E873
\1 Matches the same string that was matched by /\1 E65
the first sub-expression in \( and \). {not in Vi}
Example: "\([a-z]\).\1" matches "ata", "ehe", "tot", etc.
\2 Like "\1", but uses second sub-expression, /\2
... /\3
\9 Like "\1", but uses ninth sub-expression. /\9
Note: The numbering of groups is done based on which "\(" comes first
in the pattern (going left to right), NOT based on what is matched
first.
You can use
:%s/if(!\(.*\))/if(\1 == NULL)/gc
By putting .* in \( \) you make numbered captured group, which means that the regex will capture what is in .*
When the replace starts then by using \1 you will print the captured group.
A macro is easy in this case, just do the following:
qa .............. starts macro 'a'
f! .............. jumps to next '!'
x ............... erase that
e ............... jump to the end of word
a ............... starts append mode (insert)
== NULL ........ literal == NULL
<ESC> ........... stop insert mode
q ............... stops macro 'a'
:%norm #a ........ apply marco 'a' in the whole file
:g/^if(!/ norm #a apply macro 'a' in the lines starting with if...
Try the following:
%s/if(!\(.\{-}\))/if(\1 == NULL)/gc
The quantifier .\{-} matches a non-empty word, as few as possible (more strict than .*).
The paranthesis \( and \) are used to divide the searched expression into subexpressions, so that you can use those subgroups in the substitute string.
Finally, \1 allows the user to use the first matched subexpression, in our case it is whatever is caught inside the paranthesis.
I hope this is more clear, more information can be found here. And thanks for the comment that suggests improving the answer.
I am using Vim, and I have the following code:
print "Number 1 = $no1\n";
print "Number 2 = $no2\n";
When I apply the following substitute command
$s/.*\(\d\\n\)\#<=\(";\)/\1
the result is
1\n
2\n
and when I substitute with backreference \2 instead
$s/.*\(\d\\n\)\#<=\(";\)/\2
the result is
";
";
I thought that I only have one backreference in the regex (the ";) What was stored in \1 appears to be the regex I used within my zero-width positive lookbehind, which I thought would NOT be stored in a backreference.
Am I mistaken?
I think \( is always a capturing back reference. From what I can see from a few attempts, what you want is a \%(, which is a non-capturing back reference.
So basically, rewriting your substitute as:
$s/.*\%(\d\\n\)\#<=\(";\)/\1
will put
";
to backreference \1, rather than \2
When you apply the following substitute command:
:%s/.*\(\d\\n\)\#<=\(";\)/\1
... the result is:
1\n
2\n
As should be expected, because you've captured the below expression in the \1 capturing group:
\(\d\\n\)
... and when you substitute with backreference \2 instead
:%s/.*\(\d\\n\)\#<=\(";\)/\2
... the result is:
";
";
As should be expected, because you've captured the below expression in the second capturing group:
\(";\)
I'm unclear what you're trying to do. What output were you expecting from the above substitutions?