-1

how to find lines that contain both specified words - using Regex pattern?

For example, I need find lines, than contains both words 'MExp' and 'Aggregate'.

Thank you.

bmi
  • 652
  • 2
  • 10
  • 17
  • What programming language are you using? – lemon Jan 30 '23 at 16:40
  • I assume that Regex is more or less standardized. So it's primarily about the syntax of searching for two words at the same time in one line. That's how the question sounded. – bmi Jan 30 '23 at 20:08

3 Answers3

1

Well, "both words" is equivalent to "word1...word2" or "word2...word1":

(word1.*word2)|(word2.*word1)

Or, depending on your environment/host language, simply use two regex, e.g. with JavaScript:

if (line.test(/word1/) && line.test(/word2/)) {
  // both words
}

or with grep:

<line.txt grep 'word1' | grep 'word2'
knittl
  • 246,190
  • 53
  • 318
  • 364
  • Yes, thank you, this helped me find such lines in two common searching tool which I tried. – bmi Jan 30 '23 at 20:11
1

To match 1 word: \b(Aggregate)\b. If you don't have to be super efficient then I would test 2 times.

Also this tool is useful for generating regex fast: regex generator

I could not research this furthermore, but this topic might help you out combined with the word search.

Good luck! ;)

1

Let's say you have a file.

abcde
fghijk MExp lmnopq Aggregate
fghijk MExp Aggregate lmnopq 
fghijk Aggregate lmnopq MExp 
fghijk Aggregate MExp lmnopq  
xywz

If the order of the words are not relevant, the regex would be

(MExp.*Aggregate)|(Aggregate.*MExp)

If the order is important, use only the applicable one.

MExp.*Aggregate or Aggregate.*MExp

ap-osd
  • 2,624
  • 16
  • 16
  • Yes, this is my case. I have hundreds of files and I need to find occurrences of lines in them that contain both words. The search tools I've tried work fine with this pattern. – bmi Jan 30 '23 at 20:14
  • I was trying to find the AND operator in the Regex documentation. According to the answers, it looks like the Regex has no such operator. – bmi Jan 30 '23 at 20:21
  • @bmi AND is the _default_ operator. The regex `AB` finds `A` AND `B` (in that order). `A|B` finds `A` OR `B`. And `A.*B` finds `A` AND "anything" AND `B` (in that order). – knittl Jan 30 '23 at 20:33