Skip to content

Commit d91f56e

Browse files
jorendorffrachaelrenksophietheking
authored
Document escape sequences in code search (#40370)
Co-authored-by: Rachael Rose Renk <91027132+rachaelrenk@users.noreply.github.com> Co-authored-by: Sophie <29382425+sophietheking@users.noreply.github.com>
1 parent 1b004c8 commit d91f56e

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

content/search-github/github-code-search/understanding-github-code-search-syntax.md

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,26 @@ To search for an exact string, including whitespace, you can surround the string
5151
"sparse index"
5252
```
5353

54-
To search for a phrase containing a quotation mark, you can escape the quotation mark using a backslash. For example, to find the exact string `name = "tensorflow"`, you can search:
54+
You can also use quoted strings in qualifiers, for example:
5555

5656
```text
57-
"name = \"tensorflow\""
57+
path:git language:"protocol buffers"
5858
```
5959

60-
You can also use quoted strings in qualifiers, for example:
60+
## Searching for quotes and backslashes
61+
62+
To search for code containing a quotation mark, you can escape the quotation mark using a backslash. For example, to find the exact string `name = "tensorflow"`, you can search:
6163

6264
```text
63-
path:git language:"protocol buffers"
65+
"name = \"tensorflow\""
6466
```
6567

68+
To search for code containing a backslash, `\`, use a double backslash, `\\`.
69+
70+
The two escape sequences `\\` and `\"` can be used outside of quotes as well. No other escape sequences are recognized, though. A backslash that isn't followed by either `"` or `\` is included in the search, unchanged.
71+
72+
Additional escape sequences, such as `\n` to match a newline character, are supported in regular expressions. See "[Using regular expressions](#using-regular-expressions)."
73+
6674
## Using boolean operations
6775

6876
Code search supports boolean expressions. You can use the operators `AND`, `OR`, and `NOT` to combine search terms.
@@ -290,3 +298,21 @@ Note that you'll have to escape any forward slashes within the regular expressio
290298
```text
291299
/^App\/src\//
292300
```
301+
302+
Inside a regular expression, `\n` stands for a newline character, `\t` stands for a tab, and `\x{hhhh}` can be used to escape any Unicode character. This means you can use regular expressions to search for exact strings that contain characters that you can't type into the search bar.
303+
304+
Most common regular expressions features work in code search. However, "look-around" assertions are not supported.
305+
306+
## Separating search terms
307+
308+
All parts of a search, such as search terms, exact strings, regular expressions, qualifiers, parentheses, and the boolean keywords `AND`, `OR`, and `NOT`, must be separated from one another with spaces. The one exception is that items inside parentheses, `(` `)`, don't need to be separated from the parentheses.
309+
310+
If your search contains multiple components that aren't separated by spaces, or other text that does not follow the rules listed above, code search will try to guess what you mean. It often falls back on treating that component of your query as the exact text to search for. For example, the following query:
311+
312+
```text
313+
printf("hello world\n");
314+
```
315+
316+
Code search will give up on interpreting the parentheses and quotes as special characters and will instead search for files containing that exact code.
317+
318+
If code search guesses wrong, you can always get the search you wanted by using quotes and spaces to make the meaning clear.

0 commit comments

Comments
 (0)