Training, Open Source computer languages

PerlPythonMySQLTclRubyC & C++LuaJavaTomcatPHPhttpdLinux

Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Python Regular Expressions

Python supports string pattern matching to regular expressions, using Perl style regular expressions.

The re module - loaded via
  import re
brings the appropriate elements into your program ... ready to use.

Patterns that you want to match against are called Regular Expressions and are created as objects from strings using the re.compile method. For example
  looklike = r'([a-z]+)\.([a-z]+)\[([^\]]+)\]'
  el_matcher = re.compile(looklike,re.IGNORECASE)


And you can then use the compiled regular expressions to find parts of data strings that match the pattern you have defined:
  zz = el_matcher.findall(thing)

If you're new to regular expressions, you'll probably find the definition strings - the regular expressions - a bit overpowering at first> but as you read into them the become easier. Let's analyse the major elements of the one above:

[a-z] A letter in the range a to z
+ One or more (of the previous item)
\. Literally a full stop
[a-z] A letter in the range a to z
+ One or more (of the previous item)
\[ Literally a [
[^\] Any character which is NOT a ]
+ One or more (of the previous item)
\] Literally a ]

Read that against sphere.v[32:58] and you'll see that it does indeed meet the description ... the extra round brackets in the original regular expression show Python which particular parts of the match are to be stored into separate objects that are returned from the match so that use can be made of them in subsequent lines of the program.

The complete example that I've been quoting from above - which matches a string of the type that may be produced by Autocad's Maya, for example, may be found here ... and we cover regular expressions on many of our courses (including our Python course, and indeed we run a special Regular Expression Course for those who need detailed coverage.

(written 2008-11-08 07:35:31)

 
Associated topics are indexed under
Y108 - Python - String Handling
Y115 - Additional Python Facilities

Back to
What are exceptions - Python based answer
Previous and next
or
Horse's mouth home
Forward to
Guests doing a bunk?
Some other Articles
Melksham Chamber of Commerce, or the FSB
Dynamic code - Python
Pascals Triangle in Python and Java
Guests doing a bunk?
Python Regular Expressions
What are exceptions - Python based answer
Is it worth it?
List Comprehensions in Python
Barack Obama wins US Predidency
Optional and named parameters in Python
1975 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2009: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho