Regular expressions

Seymore4Head Seymore4Head at Hotmail.invalid
Thu Nov 5 10:07:06 EST 2015


On Thu, 05 Nov 2015 11:54:20 +1100, Steven D'Aprano
<steve at pearwood.info> wrote:

>On Thu, 5 Nov 2015 10:02 am, Seymore4Head wrote:
>
>> So far the only use I have for regex is to replace slicing, but I
>> think it is an improvement.
>
>I don't understand this. This is like saying "so far the only use I have for
>a sandwich press is to replace my coffee pot". Regular expressions and
>slicing do very different things.
>
>Slicing extracts substrings, given known starting and ending positions:
>
>
>py> the_str = "Now is the time for all good men..."
>py> the_str[7:12]
>'the t'
>
>
>Regular expressions don't extract substrings with known start/end positions.
>They *find* matching text, giving a search string with metacharacters. (If
>there are no metacharacters in your search string, you shouldn't use a
>regex. str.find will be significantly faster and more convenient.)
>
>Slicing is not about finding text, it is about extracting text once you've
>already found it. So they are complementary, not alternatives.

Here is an example of the text we are slicing apart.

>From stephen.marquard at uct.ac.za Sat Jan  5 09:14:16 2008
Return-Path: <postmaster at collab.sakaiproject.org>
Received: from murder (mail.umich.edu [141.211.14.90])
	 by frankenstein.mail.umich.edu (Cyrus v2.3.8) with LMTPA;
	 Sat, 05 Jan 2008 09:14:16 -0500
X-Sieve: CMU Sieve 2.3
Received: from murder ([unix socket])
	 by mail.umich.edu (Cyrus v2.2.12) with LMTPA;
	 Sat, 05 Jan 2008 09:14:16 -0500
Received: from holes.mr.itd.umich.edu (holes.mr.itd.umich.edu
[141.211.14.79])
	by flawless.mail.umich.edu () with ESMTP id m05EEFR1013674;
	Sat, 5 Jan 2008 09:14:15 -0500
Received: FROM paploo.uhi.ac.uk (app1.prod.collab.uhi.ac.uk
[194.35.219.184])
	BY holes.mr.itd.umich.edu ID 477F90B0.2DB2F.12494 ; 
	 5 Jan 2008 09:14:10 -0500
Received: from paploo.uhi.ac.uk (localhost [127.0.0.1])
	by paploo.uhi.ac.uk (Postfix) with ESMTP id 5F919BC2F2;
	Sat,  5 Jan 2008 14:10:05 +0000 (GMT)
Message-ID: <200801051412.m05ECIaH010327 at nakamura.uits.iupui.edu>
Mime-Version: 1.0
Content-Transfer-Encoding: 7bit
Received: from prod.collab.uhi.ac.uk ([194.35.219.182])
          by paploo.uhi.ac.uk (JAMES SMTP Server 2.1.3) with SMTP ID
899
          for <source at collab.sakaiproject.org>;
          Sat, 5 Jan 2008 14:09:50 +0000 (GMT)
Received: from nakamura.uits.iupui.edu (nakamura.uits.iupui.edu
[134.68.220.122])
	by shmi.uhi.ac.uk (Postfix) with ESMTP id A215243002
	for <source at collab.sakaiproject.org>; Sat,  5 Jan 2008
14:13:33 +0000 (GMT)
Received: from nakamura.uits.iupui.edu (localhost [127.0.0.1])
	by nakamura.uits.iupui.edu (8.12.11.20060308/8.12.11) with
ESMTP id m05ECJVp010329
	for <source at collab.sakaiproject.org>; Sat, 5 Jan 2008 09:12:19
-0500
Received: (from apache at localhost)
	by nakamura.uits.iupui.edu (8.12.11.20060308/8.12.11/Submit)
id m05ECIaH010327
	for source at collab.sakaiproject.org; Sat, 5 Jan 2008 09:12:18
-0500
Date: Sat, 5 Jan 2008 09:12:18 -0500
X-Authentication-Warning: nakamura.uits.iupui.edu: apache set sender
to stephen.marquard at uct.ac.za using -f
To: source at collab.sakaiproject.org
From: stephen.marquard at uct.ac.za

The practice problems are something like pull out all the email
addresses or pull out the days of the week and give the most common.




More information about the Python-list mailing list