iOS Shortcuts match regex not handling NOTs

Running on iOS 16.0 (20A362)


For a bit of fun, i thought I’d try to come up with a shortcut to help play an optimum game of wordle. I have the words in a txt file, each word on its own line. For example:


feast

foist

fried

foray

fancy

froze

fence

fleet

forty

front

fable

forge

frost

facet

flute

flaky

fatty

ferry

folly


I ask for input to ascertain the letters guessed correctly in the correct positions. So for example my regex would correctly tell me that if i guess “ferry” using the sample list above, and i tell the shortcut “fe” were correct and in correct position, it would then tell me that the next best guesses would be “fence” and “feast”. I do this using a loop to first match pattern “f….” , then update the word list with those matches, before next matching pattern “.e…” on the revised list before updating the list again. So far so good!

Next I ask for correct letters that were in the wrong place, and loop through those using “(?=.*Repeat Item).+”. Again this works great BUT, it isn’t enough, as it returns ALL words containing those letters in ANY position - but I also need to exclude those letters from the positions they were in as i know they are NOT there. For example, if when i guessed “ferry” the result had been no letter placed correctly, but just the “f” guessed correctly but in the wrong place, I need to remove all words with “f” in the first letter position and then find all the remaining words that have “f” in position 2,3,4 or 5. This is where things go wrong. If i use the pattern “f….” I will get back a list of all the words starting with “f”. If i then try “[^f]….” I get an error. “There was a problem running your shortcut”. I can’t see what is wrong with my pattern. Similarly, i have tried using the negative lookahead pattern also, and that didn’t work either (but positive lookahead was fine). For example looping though this is fine


(?=.*Repeat Item).+


but this does nothing


(?!.*Repeat Item).+


Anyone got any idea what i am doing wrong here please?



iPhone 12 mini

Posted on Sep 21, 2022 12:58 AM

Reply
3 replies

Sep 21, 2022 8:33 AM in response to M-Ray

Regex is a pattern matching tool, and somewhat of a blunt instrument. If you want a character in (or not in) a specific position, you’ll have to wildcard a specific number of characters ahead of that regex match, and quite probably padding out single-character wildcards (Shortcuts uses the period (.) for that) on both sides of the match for the chosen length of the target string.


As for the error… You’ll want to create and submit a concise reproducer of this apparent bug, and submit it to the folks at Apple.


As for more a general algorithm, search for the paper An Exact and Interpretable Solution to Wordle by MIT professors Dimitris Bertsimas and Alex Paskov. There are undoubtedly other algorithms posted around the ‘net, too.


I’d probably use Swift or maybe Python for a solver algorithm. Shortcuts and its regex support wouldn’t be my choice for heavy string handling.


Here’s a reasonable-looking intro to Shortcuts and its use of regex, for those following along at home:


https://appletoolbox.com/how-to-start-using-regex-with-the-shortcuts-app/


Sep 21, 2022 10:45 AM in response to MrHoffman

Thanks MrHoffman, that's very kind of you. Going to keep away from those MIT links though (for now) as figuring out a good approach is half the fun! I think you are quite right about using Swift or Python for this - it is just a bit too heavy duty for Shortcuts I suspect - but again, was just trying this for fun when I noticed that Shortcuts 'supports' the use of Regex. I'll raise a bug as something weird is definitely happening here - and when I should have said, when I test my regex attempts in Regex101.com they seem OK.


This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

iOS Shortcuts match regex not handling NOTs

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.