From 17f4bfe56dcda74f5bef4eb7eeb62c2f85acab4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nuno=20Andr=C3=A9?= Date: Fri, 12 Feb 2021 02:11:22 +0100 Subject: [PATCH 1/2] Create README.md Using cooked literals as regex patterns makes the baby Jesus cry. --- README.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/README.md b/README.md index 9cadd2a..3498a7f 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ So, here we go... + [▶ All-true-ation *](#-all-true-ation-) + [▶ The surprising comma](#-the-surprising-comma) + [▶ Strings and the backslashes](#-strings-and-the-backslashes) + + [▶ Blurred boundaries](#-blurred-boundaries) + [▶ not knot!](#-not-knot) + [▶ Half triple-quoted strings](#-half-triple-quoted-strings) + [▶ What's wrong with booleans?](#-whats-wrong-with-booleans) @@ -1351,6 +1352,42 @@ True ``` - This means when a parser encounters a backslash in a raw string, it expects another character following it. And in our case (`print(r"\")`), the backslash escaped the trailing quote, leaving the parser without a terminating quote (hence the `SyntaxError`). That's why backslashes don't work at the end of a raw string. +--- +### ▶ Blurred boundaries +```py +>>> re.match('wtf', 'wtfwtf') + +>>> re.match('wtf', 'wtf') + +``` + +- `\B` matches the empty string, but only when it **_is not_** at the beginning or end of a word. +```py +>>> re.match('wtf\B', 'wtfwtf') + +>>> re.match('wtf\B', 'wtf') +None +``` + +- `\b` matches the empty string, but only when it **_is_** at the beginning or end of a word. + +```py +>>> re.match('wtf\b', 'wtfwtf') +None +>>> re.match('wtf\b', 'wtf') +None +``` + +#### 💡 Explanation +- `\b` and `\B` are anchor symbols of the regular expression syntax. But, unlike `\B`, `\b` is also a valid escape as a string literal (the backspace character). Thus in order for the `re` lexer to interpret the word boundary, it has to be escaped when is not used within raw literals. +```py +>>> re.match('wtf\\b', 'wtf') + +>>> re.match(r'wtf\b', 'wtf') + +``` + + --- ### ▶ not knot! From c3a752609601eeff250fd90eea522df7f7b41603 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nuno=20Andr=C3=A9?= Date: Fri, 12 Feb 2021 02:17:13 +0100 Subject: [PATCH 2/2] Update CONTRIBUTORS.md --- CONTRIBUTORS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 28c3890..d7efe0b 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -24,7 +24,7 @@ Following are the wonderful people (in no specific order) who have contributed t | Jongy | [Jongy](https://github.com/Jongy) | [#208](https://github.com/satwikkansal/wtfpython/issues/208), [#210](https://github.com/satwikkansal/wtfpython/issues/210), [#233](https://github.com/satwikkansal/wtfpython/issues/233) | | Diptangsu Goswami | [diptangsu](https://github.com/diptangsu) | [#193](https://github.com/satwikkansal/wtfpython/issues/193) | | Charles | [charles-l](https://github.com/charles-l) | [#245](https://github.com/satwikkansal/wtfpython/issues/245) - +| Nuno André | [nuno-andre](https://github.com/nuno-andre) | [#247](https://github.com/satwikkansal/wtfpython/issues/245) --- **Translations**