I'm using a program that let me search between internal documents using regular expressions, what I should use if I want to search for the documents that have only and exactly a certain group of words and nothing more.
For example: the words "black" "white" and "red" and nothing else. How do I include certain group of words and exclude all the others? The words functions as tags so I must use something that indicates "black" "white" and "red" and not "black white red"
The program is called Scrivener and uses the QT5 Regular Expression engine.
CodePudding user response:
/^((\W|\b) (black|white|red)(\W|\b) )*$/gmi
This will do a multi-line, case-insensitive match.
It will allow spaces and other non-word characters between the words black, red, or white. It will not allow e.g. the word "blackred", and will not allow any other words to appear.
To allow only spaces, and not any other kinds of non-word characters (such as commas or other punctuation), replace \W
with \s
.
CodePudding user response:
Your example is quite absurd. I don't understand what you exactly what. Please add some input/output.
According to my understanding if there is input text like:
`There are 3 Black balls and 5 white balls` → output: 'black', 'white'
'There are some black white red balls' → output: null
If you want this create a regular expressions like:
/(?<!(red|black|white)\s)(red|black|white)(?!\s(red|black|white))/gim
let ignoreCase = ['red', 'black', 'white']
let regx = new RegExp('(?<!(' ignoreCase.join('\|') ')\\s)(' ignoreCase.join('\|') ')(?!\\s(' ignoreCase.join('\|') '))', 'gmi')
'white ball and red ball'.match(regx) // ['white', 'red']
'white ball red ball'.match(regx) // ['white', 'red']
'white black red ball'.match(regx) // null