Backreferencing with RE problem

Andrew Kuchling akuchlin at mems-exchange.org
Mon Apr 2 12:36:00 EDT 2001


[Your mailer base-64 encoded your e-mail, which made it unreadable in
the newsgroup.  Please explore your mailer's configuration and fix this.]

vio <vmilitaru at sympatico.ca> writes:
>gets nothing. The reference docs
>(www.python.org/doc/current/lib/module-re.html) talk about
>"expand" and "group". But if I do:

.group() and .expand() are methods of the MatchObject you get back  
from a successful search() or match() call.  The usual pattern for using a 
regex is:

m = regexp1.search('string being matched against')
if m is None:
    # No match; report an error?  do nothing?
    ...
else:
    # It did match; 'm' is now a MatchObject.
    myBackref1 = m.group(1)
    
>(Question 2: is there a magic spell to cast a variable, from an
>'object' into a 'string', for example. )

An arbitrary cast, from anything to anything?  No.  There are specific
conversions: str(foo) converts to a string, int(foo) converts to an
integer, and float(), long() behave similarly.  All of these functions
raise a TypeError exception if the argument can't be converted; for
example, int({1:2}) raises a TypeError because converting a dictionary
to an integer isn't really meaningful.

--amk



More information about the Python-list mailing list