6 Replies Latest reply on Oct 21, 2016 9:42 AM by Peter Spier

    Regex for Logical AND

    ariva38817767

      Hi,

       

      In regex same like logical OR(|), I wanted to perform logical AND operation.  For example

       

      asdfdf book asdfasdf pen asdfdsf

      dsffds book sdfasdfdsaf asdfds

      sdfdf pen dsfdaf sdafdsf

      dsfdsf pen adsfdsf book

       

      if book & pen or pen & book matched only I wanted to process further.

      if in any line only book or only pen is there nothing I want.

       

      Can any one help on this.

       

      Ariv

        • 1. Re: Regex for Logical AND
          Obi-wan Kenobi Adobe Community Professional

          Hi Ariv,

           

          This first regex will only find paras with the 2 words "book" and "pen"!

          it doesnt if not or only if one of them!

           

          (?=.*?\bbook\b).*?\bpen\b.*

           

          This second one will only find paras with the 2 words "book" and "pen" and the words group "cup of coffee"!

          it doesnt if not or only if one of them!

           

          (?=.*?\bbook\b)(?=.*?\bpen\b).*?cup of coffee.*

           

          (^/)

          1 person found this helpful
          • 2. Re: Regex for Logical AND
            Peter Spier Most Valuable Participant (Moderator)

            I believe this one will find either:

            book[^\r]+?pen|pen[^\r]+?book

            • 3. Re: Regex for Logical AND
              Peter Spier Most Valuable Participant (Moderator)

              Though it might be a good idea to add word boundaries to avoid finding "open your notebooks."

               

              So \bbook\b[^\r]+?\bpen\b|\bpen\b[^\r]+?\bbook\b

               

              Not as elegant as Obi-wan's but easier to understand, I think.

              • 4. Re: Regex for Logical AND
                Obi-wan Kenobi Adobe Community Professional

                Peter,

                 

                Really less cool and doable with this list:

                 

                fairy / demon(s) / witch / fire / hell / night / ghost / Halloween

                 

                (^/) 

                 

                Code:

                (?=.*?\bfairy\b)(?=.*?\bdemons?\b)(?=.*?\bwitch\b)(?=.*?\bfire\b)(?=.*?\bhell\b)(?=.*?\bnight\b)(?=.*?\bghost\b).*?Halloween\b.*

                • 5. Re: Regex for Logical AND
                  Skemicle Level 3

                  Peter, your codes will only select anything between the two words not the full paragraph. you would need to add '.*?' to the beginning and end of both strings for the full paragraph.. quite a bit more coding than Obi-wans solution, but until I saw Obi-wan's expression that is how I would have done it as well . Also when i first saw Obi-wan's expression my first thought was "Yeah but that only finds the paragraph if the word 'book' comes before 'pen'. Though upon investigating the order doesn't matter seeing as how the search for each word starts at the beginning of the paragraph and not from the last word found.

                  • 6. Re: Regex for Logical AND
                    Peter Spier Most Valuable Participant (Moderator)

                    My presumption is that you would apply a paragraph style, and for that you don't need to select the entire paragraph.