Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a regex that matches two or more specific whole words?

Purpose

The purpose of that RegEx is to use it on Sublime Text 3's "Search in Files" function that have support to Regular Expressions.

Goal

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.

My deductions

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.

Example:

I have many of files that are named ListaController, and i want to find only the ones that have requires: inside of it.

ListaController is the name of the files that i want to search.

requires: is the word i want to find insile files that are ListaController'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.

What i've tried

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.

like image 345
Paulo Roberto Rosa Avatar asked Nov 17 '25 06:11

Paulo Roberto Rosa


2 Answers

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.)
like image 177
Wiktor Stribiżew Avatar answered Nov 18 '25 21:11

Wiktor Stribiżew


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

like image 30
Gabriel Chiquini Avatar answered Nov 18 '25 20:11

Gabriel Chiquini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!