1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 23:28:02 +00:00

Add Equivalence Classes Of Repetition MetaChars as a sed til

This commit is contained in:
jbranchaud
2021-03-28 11:04:07 -05:00
parent 0666d49c94
commit cac816b3fc
2 changed files with 47 additions and 1 deletions

View File

@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud). For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
_1100 TILs and counting..._ _1101 TILs and counting..._
--- ---
@@ -948,6 +948,7 @@ _1100 TILs and counting..._
### sed ### sed
- [Apply Multiple Substitutions To The Input](sed/apply-multiple-substitutions-to-the-input.md) - [Apply Multiple Substitutions To The Input](sed/apply-multiple-substitutions-to-the-input.md)
- [Equivalence Classes Of Repetition MetaChars](sed/equivalence-classes-of-repetition-metachars.md)
- [Extract Value From Command Output With Sed](sed/extract-value-from-command-output-with-sed.md) - [Extract Value From Command Output With Sed](sed/extract-value-from-command-output-with-sed.md)
- [Grab All The Method Names Defined In A Ruby File](sed/grab-all-the-method-names-defined-in-a-ruby-file.md) - [Grab All The Method Names Defined In A Ruby File](sed/grab-all-the-method-names-defined-in-a-ruby-file.md)
- [OSX sed Does Regex A Bit Different](sed/osx-sed-does-regex-a-bit-different.md) - [OSX sed Does Regex A Bit Different](sed/osx-sed-does-regex-a-bit-different.md)

View File

@@ -0,0 +1,45 @@
# Equivalence Classes Of Repetition MetaChars
There are two types of Repetition MetaChars. The simple ones are `*`, `+`, and
`?`. The general ones are ranges specified inside `{` and `}`. Here are
equivalence classes between these two sets.
_These use the -E (extended regex) option for OSX's `sed`._
1. `*` is equivalent to `{0,}`
_Zero or more of the preceeding character._
```bash
$ echo 'abc123' | sed -E 's/[[:alpha:]]*/!/'
!123
$ echo 'abc123' | sed -E 's/[[:alpha:]]{0,}/!/'
!123
```
2. `+` is equivalent to `{1,}`
_One or more of the preceeding character._
```bash
$ echo 'fix the spacing' | sed -E 's/[ ]+/ /g'
fix the spacing
$ echo 'fix the spacing' | sed -E 's/[ ]{1,}/ /g'
fix the spacing
```
3. `?` is equivalent to `{0,1}`
_Exactly zero or one of the preceeding character._
```bash
$ echo '#1, 2, 1oz' | sed -E 's/#?1/ONE/g'
ONE, 2, ONEoz
$ echo '#1, 2, 1oz' | sed -E 's/#{0,1}1/ONE/g'
ONE, 2, ONEoz
```
[source](https://www.goodreads.com/book/show/19407377-definitive-guide-to-sed)