How to search on GitHub to get exact string matches, including special characters

SearchGithub

Search Problem Overview


I can search exact matches from Google by using quotes like "system <<-".

How can I do the same thing for GitHub?

Search Solutions


Solution 1 - Search

You couldn't (before 2022). The official GitHub searching rules:

> Due to the complexity of searching code, there are a few restrictions > on how searches are performed: > > * Only the default branch is considered. In most cases, this will be the master branch. > * Only files smaller than 384 KB are searchable. > * Only repositories with fewer than 500,000 files are searchable. > * You must always include at least one search term when searching source code. For example, searching for language:go is not valid, while amazing language:go is. > * At most, search results can show two fragments from the same file, but there may be more results within the file. > * You can't use the following wildcard characters as part of your search query:
> . , : ; / \ ` ' " = * ! ? # $ & + ^ | ~ < > ( ) { } [ ]
> The search will simply ignore these symbols.

Update: GitHub supports literal strings now, but you can also try some more powerful ways below.


Try Sourcegraph

For complex search with regex support try Sourcegraph.

enter image description here


Clone and use git-grep:

git support searching in sources with git-grep command. Just clone a repository and use the command in the folder:

git grep "text-to-search"

Alternatives:

I recommend you to try ripgrep tool, it's fast and simple. Works like git-grep but looks nicer:

rg "text-to-search"

And you can use the standard grep to search any text in files:

grep -r "text-to-search" /repository

Solution 2 - Search

You can use Google directly.

How about this?

"your_string_to_search" site::https://github.com
"your_string_to_search" site::https://gist.github.com

Solution 3 - Search

Today I was trying to look for an exact match of filter class in files named logback.xml in any repo on Github. And I came up with the following query which did the job.

"filter class" in:file filename:logback.xml

To enable exact matches with quotes you need to follow your search with the "in:file" modifier. The matches are not quite exact, the word "class" will have to follow the word "filter", but it seems there can be 0 or more spaces or symbols characters between the two words.

Solution 4 - Search

Adding to @[mrgloom][2]'s answer, if you're looking for code in a specific programming language in Github using Google you could do something like this in Google's search bar:

  • state the specific string you're looking for using the "intext:" search operator
  • add the programming language you're interested in, using the "ext:" operator (i.e. "ext:py", "ext:R", "ext:rb", etc.)
  • search in all public repos in Github using the "site:" operator mrgloom mentioned.

Example:

intext:"%% 2 == 0" ext:R site:github.com

[![Google Results from the example][1]][1] [1]: https://i.stack.imgur.com/YakwC.jpg [2]: https://stackoverflow.com/users/1179925/mrgloom

Solution 5 - Search

  1. Open a repository on GitHub, for example microsoft/fluentui
  2. Press dot "." to open VS Code web interface
  3. Go to search in the left panel
  4. Enable indexing via the prompt below search bar
  5. Huraaay!  exact search works

Solution 6 - Search

You can: Since Dec. 2021, your search, done from cs.github.com, can include special characters

> ## Improving GitHub code search
> (from Pavel Avgustinov) > > Search for an exact string, with support for substring matches and special characters, or use regular expressions (enclosed in / separators).

So "system <<-" should work, on that new search site.

Solution 7 - Search

If your package is in debian, you can use their code search, which supports regular expressions: https://codesearch.debian.net/

Solution 8 - Search

As of 11/2/2021, this is possible by putting quotation marks around your search string

Without quotes: Searching chaos monkey on GitHub with unquoted terms

With quotes: Searching chaos monkey on GitHub with string

While it's now possible to search exact strings, the functionality doesn't yet support searching on special characters. Example:

Searching chaos monkey on GitHub with question mark in quoted string

Solution 9 - Search

If your search term is a filename or other substring which contains punctuation characters, a partial workaround to get GitHub's code search to return instances of that substring is to (1) replace the punctuation characters in your search term with spaces, and (2) enclose the search term in quotes.

For example, instead of using the search term:

  • repo:my_repo my_image_asset_1.svg

Try:

  • repo:my_repo "my image asset 1 svg"

This might not be a perfect solution in all cases; I imagine it might also match filenames like my-image-asset-1.svg. But depending on your use case, it might be "good enough"?

Solution 10 - Search

If you quickly want to search inside a specific repo, try that:

  • Press . while viewing the repo to open it inside a browser-based VS Code window

  • Enter your search term into the menu on the left

  • Enable indexing
    enter image description here

Solution 11 - Search

I think $ dollar sign works. You should put it at the beginning and end of the search string. Without $ sign

With $ sign

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJust a learnerView Question on Stackoverflow
Solution 1 - SearchDenisKolodinView Answer on Stackoverflow
Solution 2 - SearchmrgloomView Answer on Stackoverflow
Solution 3 - SearchcessationoftimeView Answer on Stackoverflow
Solution 4 - SearchsilviaegtView Answer on Stackoverflow
Solution 5 - SearchDraex_View Answer on Stackoverflow
Solution 6 - SearchVonCView Answer on Stackoverflow
Solution 7 - SearchJan KatinsView Answer on Stackoverflow
Solution 8 - SearchJacob ArchambaultView Answer on Stackoverflow
Solution 9 - SearchJon SchneiderView Answer on Stackoverflow
Solution 10 - SearchgarzjView Answer on Stackoverflow
Solution 11 - SearchheisenbergView Answer on Stackoverflow