From cac816b3fc6733b2cf8c6cac31c7cde76d5ef9c2 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Sun, 28 Mar 2021 11:04:07 -0500 Subject: [PATCH] Add Equivalence Classes Of Repetition MetaChars as a sed til --- README.md | 3 +- ...valence-classes-of-repetition-metachars.md | 45 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 sed/equivalence-classes-of-repetition-metachars.md diff --git a/README.md b/README.md index f0ae76c..3c7d4ab 100644 --- a/README.md +++ b/README.md @@ -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). -_1100 TILs and counting..._ +_1101 TILs and counting..._ --- @@ -948,6 +948,7 @@ _1100 TILs and counting..._ ### sed - [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) - [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) diff --git a/sed/equivalence-classes-of-repetition-metachars.md b/sed/equivalence-classes-of-repetition-metachars.md new file mode 100644 index 0000000..c0c2ce3 --- /dev/null +++ b/sed/equivalence-classes-of-repetition-metachars.md @@ -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)