Python

  1. Home
  2. Computing & Technology
  3. Python

Python Regular Expression Examples: the Escape Character or Backslash

From Al Lukaszewski, for About.com

Definition:
>>> import re
>>> x = '\nBeneath the wide and starry sky, \nDig the grave and let me lie, \nGlad did I live and gladly die, \nAnd I lay me down with a will:\n\nThis be the verse you grave for me: \nHere he lies where he longed to be, \nHome is the sailor, home from sea, \nAnd the hunter home from the hill.\n\n-- "Requiem" by Robert Louis Stevenson\n\n'
>>> print x

Beneath the wide and starry sky,
Dig the grave and let me lie,
Glad did I live and gladly die,
And I lay me down with a will:

This be the verse you grave for me:
Here he lies where he longed to be,
Home is the sailor, home from sea,
And the hunter home from the hill.

-- "Requiem" by Robert Louis Stevenson


>>> y = re.compile('\n[BDGATH]')

>>> z = re.split(y,x)

>>> for item in z:

...     print item

...

eneath the wide and starry sky,
ig the grave and let me lie,
lad did I live and gladly die,
nd I lay me down with a will:

his be the verse you grave for me:
ere he lies where he longed to be,
ome is the sailor, home from sea,
nd the hunter home from the hill.

-- "Requiem" by Robert Louis Stevenson

Back to the main index of this glossary

Explore Python

About.com Special Features

Python

  1. Home
  2. Computing & Technology
  3. Python
  4. Regular Expressions
  5. Python RegEx Examples
  6. Escape Sequences
  7. Python RegEx - Python Regular Expressions - Examples - the Escape Character or Backslash

©2009 About.com, a part of The New York Times Company.

All rights reserved.