1. Home
  2. Computing & Technology
  3. Python

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

From , former About.com Guide

Definition:
>>> import re
>>> y = re.split('(.,\s.+)', 'Home is the sailor, home from sea, and the hunter home from the hill.')
>>> print y
['Home is the sailo', 'r, home from sea, and the hunter home from the hill.', '']
>>> y = re.split('(.,\s.+?)', 'Home is the sailor, home from sea, and the hunter home from the hill.')
>>> print y
['Home is the sailo', 'r, h', 'ome from se', 'a, a', 'nd the hunter home from the hill.']
>>>
>>> y = re.split('(<.+>)', '<a href="http://www.some-url.com">some hyperlink</a>')
>>> print y
['', '<a href="http://www.some-url.com">some hyperlink</a>', '']
>>> y = re.split('(<.+?>)', '<a href="http://www.some-url.com">some hyperlink</a>')
>>> print y
['', '<a href="http://www.some-url.com">', 'some hyperlink', '</a>', '']

Back to the main index of this glossary
Explore Python
About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

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

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

All rights reserved.