1. Home
  2. Computing & Technology
  3. Python

Python Regular Expression Examples: Compound RegEx: '??'

By Al Lukaszewski, About.com

Definition:
>>> import re
>>> y = re.split('(.\S?)', 'Home is the sailor, home from sea, and the hunter home from the hill.')
>>> print y
['', 'Ho', '', 'me', '', ' i', '', 's', '', ' t', '', 'he', '', ' s', '', 'ai', '', 'lo', '', 'r,', '', ' h', '', 'om', '', 'e', '', ' f', '', 'ro', '', 'm', '', ' s', '', 'ea', '', ',', '', ' a', '', 'nd', '', ' t', '', 'he', '', ' h', '', 'un', '', 'te', '', 'r', '', ' h', '', 'om', '', 'e', '', ' f', '', 'ro', '', 'm', '', ' t', '', 'he', '', ' h', '', 'il', '', 'l.', '']
>>> y = re.split('(.\S??)', 'Home is the sailor, home from sea, and the hunter home from the hill.')
>>> print y
['', 'H', '', 'o', '', 'm', '', 'e', '', ' ', '', 'i', '', 's', '', ' ', '', 't', '', 'h', '', 'e', '', ' ', '', 's', '', 'a', '', 'i', '', 'l', '', 'o', '', 'r', '', ',', '', ' ', '', 'h', '', 'o', '', 'm', '', 'e', '', ' ', '', 'f', '', 'r', '', 'o', '', 'm', '', ' ', '', 's', '', 'e', '', 'a', '', ',', '', ' ', '', 'a', '', 'n', '', 'd', '', ' ', '', 't', '', 'h', '', 'e', '', ' ', '', 'h', '', 'u', '', 'n', '', 't', '', 'e', '', 'r', '', ' ', '', 'h', '', 'o', '', 'm', '', 'e', '', ' ', '', 'f', '', 'r', '', 'o', '', 'm', '', ' ', '', 't', '', 'h', '', 'e', '', ' ', '', 'h', '', 'i', '', 'l', '', 'l', '', '.', '']

Back to the main index of this glossary

Explore Python

More from About.com

  1. Home
  2. Computing & Technology
  3. Python
  4. Regular Expressions
  5. Python RegEx Examples
  6. Python RegEx - Python Regular Expressions - Examples - Compound RegEx: '??'

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

All rights reserved.