How to search for whole words?

I was trying to search for the word “Poli” but got too many unwanted results with words prefixed with “poli” like “politician”.

Is there a way to do this? Quotes don’t work despite Discourse support discourse saying it might - How to enforce exact matches when searching? - #10 by Overgrow - support - Discourse Meta

1 Like

Try using quotes with space before and after the word within the quotations, such as…

" Poli "

This will do an exact word search.

2 Likes

That may not work in all cases though i.e. may only find a whole word when it occurs within a sentence i.e. not at the beginning or end.

To solve this correctly may require regex support. Does anyone know whether the Discourse forum software offers regex functionality in the user search, as an advanced option if not by default?

1 Like

If you use quotation marks with no space before the word, and a space after the word; I believe it works:-
"poli "

This will allow for the word to appear at the start of a sentence as well as in a sentence.

2 Likes

If the community uses punctuation correctly, using

" Poli." " Poli?" or potentially " Poli!"

Should find those at the end of a sentence.

It night be one that needs functionality loaded by Choice.

1 Like

I think that functionality relates to censorship of “offensive” words i.e. an administrative function, not functionality available for users just searching forum content.

A forum would only enable regex functionality for users with consideration and care because an accidental or malicious regex could chew up lots of server-side CPU but without achieving much or anything at all.


Don't forget about comma e.g. "CompanyName, whom I used last year, was really sh*t".

Anyway, with a mass of additional searches for fixed strings you could eventually cover fully the basic task of searching for a single word.

1 Like

I would be very surprised that any more than a handfull of searchers on Choice would even know what a Regular Expression was and how to use one for detailed searches.
Unix weanies probably would, since Grep, Awk, and Perl are commonly used.

Just some basic logic tests would impove things as per Google searches.
And, or, not, to use with words.

Poli -policy -political is the sort of thing used to quickly refine Google search results.
Or perhaps poli and payment would narrow things down.

But, not implemented by Choice. So a moot point.

It’s site feedback. A user demonstrated a legitimate reason why the functionality is required. Choice can consider as it sees fit.

I think the problem here is “not implemented by Discourse” so Choice has no, um, choice.

One workaround for that is a check box for “match whole words only”. The effect of the check box could be twofold

  1. Convert the word into the appropriate regex: \bword\b (Javascript / Java - choose whatever is the appropriate syntax in the operating environment)
  2. Conduct the search as a regex match rather than a fixed string match.

You can see that UI approach in the Firefox built-in search within web page (if you use Firefox as your web browser).

That has the effect that it can safely be implemented without opening yourself up to a DoS attack via deliberately wasteful regex.

2 Likes