Skip to content

Latest commit

 

History

History
45 lines (30 loc) · 1017 Bytes

equivalence-classes-of-repetition-metachars.md

File metadata and controls

45 lines (30 loc) · 1017 Bytes

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.

$ echo 'abc123' | sed -E 's/[[:alpha:]]*/!/'
!123

$ echo 'abc123' | sed -E 's/[[:alpha:]]{0,}/!/'
!123
  1. + is equivalent to {1,}

One or more of the preceeding character.

$ echo 'fix   the spacing' | sed -E 's/[ ]+/ /g'
fix the spacing

$ echo 'fix   the spacing' | sed -E 's/[ ]{1,}/ /g'
fix the spacing
  1. ? is equivalent to {0,1}

Exactly zero or one of the preceeding character.

$ 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