[ddj] Dynamic backreferences?

Tarek Amr tarekamr at gmail.com
Wed Sep 25 23:50:04 UTC 2013


Hi,

Here is a python code that should do what you want

def colrepeat(text):
...     instr =  text.string[text.start()+1:text.end()-1]
...     outstr = '<'
...     for col in instr.split(','):
...             outstr = outstr + ','.join([col] * 3) + ','
...     outstr += '>'
...     return outstr
...
re.sub('\<[a-z\,]+\>', colrepeat, 'I like <red,green,blue> and
<white,black>')




On Wed, Sep 25, 2013 at 9:15 PM, Patrick Maynard <
patrickmaynard.bs at gmail.com> wrote:

> Is there a simple way to make a regular expression generate its own back
> references?
>
> For example, if I have strings "this is a <red,blue,green> set" and "this
> is a <red,brown,blue,green,brown> string" (and several other strings that
> vary in the number of color instances), and I know that the brackets will
> always be exclusively wrapped around the comma-delimited color list, can I
> write a single replacement pattern that will repeat each color three times
> before going to the next color?
>
> I realize that there are a ton of much better, non-big-hammer ways of
> doing this. I'm just curious about this as a sort of academic exercise at
> this point. Is there a way to accomplish this strictly with regex?
>
> -- Patrick
>
>
>
> _______________________________________________
> data-driven-journalism mailing list
> data-driven-journalism at lists.okfn.org
> http://lists.okfn.org/mailman/listinfo/data-driven-journalism
> Unsubscribe: http://lists.okfn.org/mailman/options/data-driven-journalism
>
>


-- 
Best Regards
Tarek Amr

http://tarekamr.appspot.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.okfn.org/pipermail/data-driven-journalism/attachments/20130926/52f8ebf8/attachment-0001.html>


More information about the data-driven-journalism mailing list