regular expressions, substituting and adding in one step?



Ok, this might look familiar. I'd like to use regular expressions to change this line:

self.source += '<p>' + paragraph + '</p>\n\n'

to read:

self.source += '<p>%s</p>\n\n' % paragraph

Now, matching the middle part and replacing it with '%s' is easy, but how would I add the extra string to the end of the line? Is it done all at once, or must I make a new regex to match?

Also, I figure I'd use a group to match the word 'paragraph', and use that group to insert the word at the end, but how will I 'retain' the state of \1 if I use more than one regex to do this?

I'd like to do this for several lines, so I'm trying not to make it too specific (i.e., matching the entire line, for example, and then adding text after it, if that's possible).

So the questions are, how do you use regular expressions to add text to the end of a line, even if you aren't matching the end of the line in the first place? Or does that entail using separate regexes that *do* do this? If the latter, how do I retain the value of the groups taken from the first re?

Thanks, hope that made some sense.
.



Relevant Pages

  • Re: regex, negations, grep, find and replace (a few questions)
    ... I do not know much regex. ... But it seems as if you define each character ... be aware that different tools may use slightly different syntaxes for the same regular expressions. ... expresion" might mean "matching everything not matching the regular expression" or, in other words, removing everything matching the regexp. ...
    (alt.os.linux)
  • Re: function to match parenthesis ( and )
    ... to have a collection with all matching subelements. ... There some case about little changes where the regex modification is ... function between the first parenthesis to match. ... >>> inappropriate places to apply regular expressions. ...
    (microsoft.public.dotnet.languages.csharp)
  • [SUMMARY] Statistician I (#167)
    ... The heart of this problem, as suggested in the quiz description, is pattern ... have the pattern matching in place, the rest of the code is pretty trivial. ... use regular expressions, or at least review their knowledge. ... Next, as indicated in the comment, square brackets surrounding text are ...
    (comp.lang.ruby)
  • [Newbie] Referring to a global variable inside a function
    ... I've built a class for parsing a user-defined list of files and matching lines with a user-defined list of regular expressions. ... regular expressions and executing associated actions.""" ... def add_files: ...
    (comp.lang.python)
  • Re: snort & regular expressions
    ... As I understand, snort uses aho-corasick, wu-manber or boyer-moore multi-pattern matching algorithms for literal strings. ... it doesn't match all regular expressions simultaneously. ...
    (Focus-IDS)