Skip to content

Instantly share code, notes, and snippets.

@yowcow
Created April 26, 2023 05:44
Show Gist options
  • Save yowcow/28ab7f328270e53fa928266549bef0e7 to your computer and use it in GitHub Desktop.
Save yowcow/28ab7f328270e53fa928266549bef0e7 to your computer and use it in GitHub Desktop.
Regexp Lookaround Memo
======================
Lookahead
---------
```
❯ echo "hoge\nfuga\nhoge1\nfuga11\nhoge111\nfuga1111" | grep -P 'hoge(?=\d+)'
hoge1
hoge111
```
Negative Lookahead
------------------
```
❯ echo "hoge\nfuga\nhoge1\nfuga11\nhoge111\nfuga1111" | grep -P 'hoge(?!\d+)'
hoge
```
Lookbehind
----------
```
❯ echo "hoge\nfuga\n1hoge1\n11fuga11\n111hoge111\n1111fuga1111" | grep -P '(?<=\d)hoge'
1hoge1
111hoge111
```
Negative Lookbehind
-------------------
```
❯ echo "hoge\nfuga\n1hoge1\n11fuga11\n111hoge111\n1111fuga1111" | grep -P '(?<!\d)hoge'
hoge
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment