I have a string that has blanks marked by a marker; (in this case, an underscore):


I can execute the following command to get the word.

DictionaryLookup["f" ~~ _ ~~ "ath" ~~ _ ~~ "r"]


How do I create and provide this pattern to the DictionaryLookup function starting from the given string?


Edit: Example of a typical use case

Starting with a phrase or sentence, for instance: "Actions speak louder than words"; and adding blanks to it randomly.

a_t_o_s _pe_k l_ude_ t_a_ w__ds

which upon Dictionary look up becomes:

enter image description here


1 Answer 1

pattern = StringReplace["f_ath_r", "_" -> Blank[]]
"f" ~~ _ ~~ "ath" ~~ _ ~~ "r"
DictionaryLookup @ pattern

Generalizing to allow __ and ___ in the input string:

toSPattern = StringReplace[p : Repeated["_", {1, 3}] :> ToExpression[p]];

{"father", "feather", "featherier", "forefather", "forgather"}
  • $\begingroup$ I hit the accept button many times, but it was frozen in disbelief. As soon as it came out of its catatonic state, it turned green. Many thanks. $\endgroup$
    – Syed
    Sep 6, 2021 at 12:34
  • $\begingroup$ @Syed, my pleasure. Thank you for the accept. $\endgroup$
    – kglr
    Sep 6, 2021 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.