manpagez: man pages & more
info sed
Home | html | info | man

File: sed.info,  Node: regexp extensions,  Next: Back-references and Subexpressions,  Prev: Character Classes and Bracket Expressions,  Up: sed regular expressions

5.6 regular expression extensions
=================================

The following sequences have special meaning inside regular expressions
(used in *note addresses: Regexp Addresses. and the ‘s’ command).

   These can be used in both *note basic: BRE syntax. and *note
extended: ERE syntax. regular expressions (that is, with or without the
‘-E’/‘-r’ options).

‘\w’
     Matches any "word" character.  A "word" character is any letter or
     digit or the underscore character.

          $ echo "abc %-= def." | sed 's/\w/X/g'
          XXX %-= XXX.

‘\W’
     Matches any "non-word" character.

          $ echo "abc %-= def." | sed 's/\W/X/g'
          abcXXXXXdefX

‘\b’
     Matches a word boundary; that is it matches if the character to the
     left is a "word" character and the character to the right is a
     "non-word" character, or vice-versa.

          $ echo "abc %-= def." | sed 's/\b/X/g'
          XabcX %-= XdefX.

‘\B’
     Matches everywhere but on a word boundary; that is it matches if
     the character to the left and the character to the right are either
     both "word" characters or both "non-word" characters.

          $ echo "abc %-= def." | sed 's/\B/X/g'
          aXbXc X%X-X=X dXeXf.X

‘\s’
     Matches whitespace characters (spaces and tabs).  Newlines embedded
     in the pattern/hold spaces will also match:

          $ echo "abc %-= def." | sed 's/\s/X/g'
          abcX%-=Xdef.

‘\S’
     Matches non-whitespace characters.

          $ echo "abc %-= def." | sed 's/\S/X/g'
          XXX XXX XXXX

‘\<’
     Matches the beginning of a word.

          $ echo "abc %-= def." | sed 's/\’
     Matches the end of a word.

          $ echo "abc %-= def." | sed 's/\>/X/g'
          abcX %-= defX.

‘\`’
     Matches only at the start of pattern space.  This is different from
     ‘^’ in multi-line mode.

     Compare the following two examples:

          $ printf "a\nb\nc\n" | sed 'N;N;s/^/X/gm'
          Xa
          Xb
          Xc

          $ printf "a\nb\nc\n" | sed 'N;N;s/\`/X/gm'
          Xa
          b
          c

‘\'’
     Matches only at the end of pattern space.  This is different from
     ‘$’ in multi-line mode.

© manpagez.com 2000-2026
Individual documents may contain additional copyright information.