MacOS – Use of regular expressions in macOS search fields

command linemacossearchtext input

Does anyone know where I can find the docs describing Regex parsing capabilities of macOS search fields?

What I mean, I'd like to read about the regex syntax rules that are allowed and parsed by the search field. For example, what I didn't know until today that, in macOS Preview (or any other application with a standard search field, guessing) we can use input like this:

"\s" AND "an" AND "\s"

This works and hints that search fields interpret more regex then we know, and this is what I want to read/find out about. The example above searches for "an" with one trailing and leading whitespace character \s within a opened document.

I'd like to know more about those grouping rules and regex characters wrapping/escaping so it can be correctly parsed. Also does this format of input apply to systemwide search fields of any application?

I can't find any docs describing these features and was found only this on developer.apple.com library instead.

Below you can see an example of default search behaviour for a "an" word within a PDF document, it shows all "an" occurences in the results, regardless if you put a space before the "an" word, so your input is " an "

enter image description here

When we put "\s" AND "an" AND "\s" into the search field we get the more desired search result

regex input search field behaviour

This makes a huge difference for searches containing common words!

In short: I'd like to know more about the functionality you see above. Where can I find any (official) documentation about it?

The answers provided (so far) are not answering my question, thank you for them though – aggregating more related information is good.

Best Answer

I don't know if you discovered this already but you just need to click on the magnifying glass in the search field, it has a little down-pointing caret which is the clue, see this screenshot:

enter image description here

After clicking choose Insert Pattern and you get a range of options:

This is basically user-friendly regex, i.e. you get tokens you can easily arrange into quite powerful search patterns.

enter image description here

The example in the screenshot is TextEdit however this option doesn't appear to be available in Preview.