Regular Expressions

Kirk Job-Sluder kirk at eyegor.jobsluder.net
Fri Aug 27 12:10:39 EDT 2004


On 2004-08-27, Oriana <oriana.falco at thalesesec.com> wrote:
> hi!
>
>   I am trying to do some replacements using regular expressions, but
> I'm not exactly sure how to do it. I need to replace all ocurrences of
>
> /******************************************         OR
> /*
>
> for /**  ....meaning, any / (forward slash) followed by one or more
> asterisks needs to be replaced with /**  ....how would I write a pieco
> of code to do this??  Please help! Thanks in advance...

#first create a regular expression object.
foo = re.compile(r"^/\*+")

#The regular expression here means match any pattern at the start 
#of a line (^) followed by a literal asterisk (\*) repeated one or 
#more times (+).  

#then use the re object to do the substitution magic.  I find the
#syntax for re.sub() to be a bit backwards myself.  It might help to
#think of it as "put the replacement into the line."

newline = foo.sub("/**",line)



>
> Ori


-- 
Kirk Job-Sluder
"The square-jawed homunculi of Tommy Hilfinger ads make every day an
existential holocaust."  --Scary Go Round



More information about the Python-list mailing list