The purpose of that RegEx is to use it on Sublime Text 3's "Search in Files" function that have support to Regular Expressions.
My goal is to achieve a search that find specific whole words on specific file names, a plus is that the filename is defined as a string inside the file itself, so it maybe will make the things more easy.
I was thinking about how to perform it, and i realize that if i can create a regex that search two whole words on same file, it solves my problem, because the filename is inside the file too.
I have many of files that are named
ListaController, and i want to find only the ones that haverequires:inside of it.
ListaControlleris the name of the files that i want to search.
requires:is the word i want to find insile files that areListaController's
So i need a RegEx that can match (ListaController and requires:) on the same file, to show only files that have the two words matched.
The most close i did reach is that RegEx:
\bListaController\b|\brequires:\b
that resulted me only ListaController whole word results and none of requires: results.
So i need some help to create a RegEx that suits.
Obs: if there is a easier/better solution to perform what i want on sublime without the use of regex, i will be grateful to see it and maybe it will be my final solution.
The \bListaController\b|\brequires:\b pattern is incorrect for the current task, because it searches for a whole word ListController or (the | is an alternation operator) requires: that is enclosed with word chars (digits, letters or _).
You want to match a file that contains both ListController and requires: as whole words, and that is possible with either alternation with .* in between the two patterns, or just using a lookahead based regex. The latter type is actually preferred since it is less cumbersome, and only requires to use a subpattern only once.
You may use
(?s)\A(?=.*?\bListaController\b)(?=.*?\brequires:)
See the regex demo.
Details
(?s) - a DOTALL mode enabling . to match line break chars that it does not match by default\A - start of the file(?=.*?\bListaController\b) - a positive lookahead that requires any 0+ chars, as few as possible (*? is a lazy quantifier), up to a whole word ListaController including it immediately to the right of the current position inside the string (that is, from the start of the file)(?=.*?\brequires:) - a positive lookahead that requires any 0+ chars, as few as possible, up to a whole word requires: including it immediately to the right of the current position inside the string (that is, again, from the start of the file since lookaheads are zero-width assertions that do not move the regex index when their patterns are matched.)If you will search a string in file, you can use a filename filter, using the "Where" feature. Place requires: in find box and *ListaController* in where box
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With