RegEx Question (word list filtered on matching prefix and suffix)

Folks... I need assistance on this one. Can someone tell me where I'm going wrong?

I'm trying to have regex select words that begin, with ae, and end, in y, then Display them.
I'm sure my regex works. However, my For Each action displays the previous match if the line doesn't contain the match.

I only want it to display: aerology, aerobiology, aerity and aeity. I only want each word displayed once. Right now, it's displaying aerology twice and aerity 3 times. I see what it's doing and why... I just don't know how to fix it. :slight_smile:

Can someone from 3000 ft give me a reality check? My attempts to clear the variable for the duplicates failed.

ae Test.kmmacros (3.2 KB)

Macro Image

image

Example text if you would like to try without pulling in the macro.

aerology
aerobe
aerobiology
aerity (this isn't a word.. I just made it up as an example)
aerobrake
early
aeity (this isn't a word.. I just made it up as an example)

Thanks for your time and help.
KC

1 Like

The macro below seems to yield:

Screenshot 2022-10-29 at 17.32.30

Subset of words with given prefix and suffix.kmmacros (6.7 KB)

1 Like

Here’s my very brief take: your RegEx is fine... you just need to clear the Line2 variable after each “execution” and then have it show you the result only if that variable is recreated.

ae Test.kmmacros (7.3 KB)

Macro screenshot (click to expand/collapse)

NOTE: I prepended all your variables with debug__ which makes it much easier to clean up afterwards.

2 Likes

Thank you Rob and Chris! You two are ALWAYS so helpful and I appreciate it.

Rob will fight against RegEx until his dying day! :rofl: - Thank you for showing a different option.

Chris, my other attempts were so close but I didn't account for the empty variable. Thank you for the much needed lesson.

I played with this macro for hours when I should have just asked (was it pride?)

Both were excellent solutions to add to the toolbox. I marked @cdthomer's guidance, as the solution, because I'm trying to incorporate Regex more. However, I bookmarked @ComplexPoint's solution, as a different option, for the kit.

Thank you both @ComplexPoint and @cdthomer for making this forum elite and for helping me!

Edited: For those interested in the final macro, please see below:

ae Test repost.kmmacros (4.4 KB)

Final Image

KC

4 Likes

Glad to help out where I can! And FWIW sometimes stubbornly tinkering with things for awhile before asking for help can still teach us a lot, so don’t consider it time wasted. :wink:

Chucking another solution out there -- this happens when the regex fails, and "No match" means the variable retains its previous value (if any). So you could check "Last action result" and act accordingly:

ae Test w action check.kmmacros (4.3 KB)

Image

1 Like

The wonderful wizard of Nige (@Nige_S) steps in to provide credible insight. Thank you for your input! The more solutions - the better.

FWIW, a Haskell solution:

Filtered word list on matching prefix and suffix.kmmacros (4.3 KB)

Haskell code:

module Main where

import Data.List

interact' :: (String -> String) -> IO ()
interact' f = putStr . f =<< readFile =<< getContents

main :: IO ()
main =
  interact' $
    unlines
      . filter
        ( (&&)
            <$> isPrefixOf "ae"
            <*> isSuffixOf "y"
        )
      . lines
1 Like