Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add regex word boundaries wtf #249

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand Down
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 object; span=(0, 3), match='wtf'>
>>> re.match('wtf', 'wtf')
<re.Match object; span=(0, 3), match='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')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All these string literals need to be raw strings

<re.Match object; span=(0, 3), match='wtf'>
>>> 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 object; span=(0, 3), match='wtf'>
>>> re.match(r'wtf\b', 'wtf')
<re.Match object; span=(0, 3), match='wtf'>
```


---

### ▶ not knot!
Expand Down