I have a large text file that I need to search for certain substrings and I would like to use the speed-up that TextSearch gives. Unfortunately, with TextSearch queries will only match entire words:

f = Export["~/test.txt", "1234 1234"];
TextSearch[CreateSearchIndex@f, SearchQueryString["*23*"]]

enter image description here

I'd prefer not to use StringCases as it is very slow on the large document. Is there some way around this?

  • $\begingroup$ Is it not Import? $\endgroup$ Commented May 3, 2018 at 23:32
  • $\begingroup$ And then after import... $\endgroup$
    – M.R.
    Commented May 3, 2018 at 23:32
  • $\begingroup$ Try maybe "23~" as the search string. ~ is supposed to indicate a close but non-exact match. It does work in your simple case, but I don't know how well it would perform on a real example. $\endgroup$
    – MarcoB
    Commented May 4, 2018 at 3:15

1 Answer 1


You may use SearchQueryString in TextSearch.

f = Export["~/test.txt", "helloworld"];
TextSearch[CreateSearchIndex@f, SearchQueryString["hello*"]]

Mathematica graphics

Hope this helps.

  • $\begingroup$ Thanks @Edmund! See my update, we've almost got it I think $\endgroup$
    – M.R.
    Commented May 4, 2018 at 1:50

Your Answer

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

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