Regular Expressions

O

Oriana

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...

Ori
 
K

Kirk Job-Sluder

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top