16

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.

6
  • Will this link be of any help to you? Dec 27, 2016 at 13:43
  • Hi Denis, thank you for the link- but this is more or less the posix regex manual ;-) and if you look carefully into my example above- its not posix regex- since the common syntax isn't working and that is why the question is here. Im not sure if words like AND OR are in posix regex (correct me, if i'm wrong) anyways. That being said- I can't find the specifics of the syntax for this kind of search field. It is (probably) an instance of developer.apple.com/reference/appkit/nssearchfield#overview (guess) but I can't dig out the regex specifics.
    – PJJ
    Dec 29, 2016 at 6:28
  • Are you sure that we’re talking about regex here? I tried your example in Preview, and a few regex variations, but none of them seemed to behave as a proper regular expression. That doesn’t answer your question, but might help in looking in the right place.
    – Manngo
    Jan 17, 2017 at 11:13
  • Yes, you are right - and this is what I want to find out! Where is a document describing what kind of syntax is parsed and how? It's not posix regex, but it's regex-ish enough, so what is it? :)
    – PJJ
    Jan 17, 2017 at 22:24
  • Actually, your example "\s" AND "an" AND "\s" above is incorrect. At least in my version of Preview (macOS 10.15), "\s" AND "an" AND "\s" and simply searching "an" (with quotes) give the exact same result with respect to finding the word "an" (surrounded by spaces). What including "AND \s" does is to add the additional requirement that an "S" (again with leading and trailing spaces) should also be present somewhere in the document, but without any restriction as to where. The backslash ("\") appears to be ignored completely.
    – gaspanic
    Nov 19, 2020 at 14:01

3 Answers 3

4

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.

1
  • 1
    Hi- thank you for the long answer, BUT this is TextEdit you are showing... not the system-default search window like the one in Preview option, therefore it is not the answer i'm looking for. The search Window I mean is (highly probable) an instance of developer.apple.com/reference/appkit/nssearchfield#overview
    – PJJ
    Dec 29, 2016 at 6:15
1

This Apple Support article on how to Narrow your search results on Mac is the most detailed documentation I have been able to find on Finder searches. While the OP is mainly asking about regular expression searches in Preview, the question title brought me here looking for the same information for Finder searches.

The last section on boolean operators (Search using AND, OR, and NOT) is relevant also for Preview, although their utility for text search is rather questionable. Regular expressions are not mentioned, so I suppose these are not supported in the macOS search fields.

1
0

This isn't regular expressions; I'm afraid you've faked yourself out. in most of Apple's document apps, there's a distinction between search for "..." (placed within quotes) ... (entered without quotes). The first will search for the enclosed text as a word; the second searches for the text without regarding word boundaries.

So, when we enter the following:

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

the parser searches for a literal word s (ignoring the backslash), then the word an then another literal word s. Since it's extremely unlikely to find 's' as an independent word, the result is exactly the same as if we had merely entered:

"an"

but different from what happens if we enter:

an

Confusing, I know...

3
  • Actually, \s is not assumed to be a control character. As I also wrote in a comment to the OPs question, "\s" searches for the word s, completely ignoring the backslash character.
    – gaspanic
    Nov 19, 2020 at 16:09
  • @gaspanic: Ah, that makes sense; thanks. I'll edit it in. Nov 19, 2020 at 16:24
  • Gentelmen, I think you are right here; I might have confused myself into thinking it is actually RE - BUT - have you made any test within recent macOS (BigSur/Catalina)?
    – PJJ
    Mar 15, 2021 at 12:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .