[ddj] Dynamic backreferences?

Aidan Kelly A.Kelly at gold.ac.uk
Thu Sep 26 09:38:15 UTC 2013


Since there is no unsubscribe option - please remove me from the list

It was interesting to me for a while but as I'm not a journalist it no longer is
Looks good for journalists however.

Aidan Kelly BA MA
Senior Lecturer in Social Research Methods
Department of Sociology,
Goldsmiths, University of London
Lewisham Way
London SE14 6NW

Email: a.kelly at gold.ac.uk
Landline: 02070785019
http://www.gold.ac.uk/sociology/staff/kelly/
________________________________
From: data-driven-journalism-bounces at lists.okfn.org [data-driven-journalism-bounces at lists.okfn.org] on behalf of Tarek Amr [tarekamr at gmail.com]
Sent: 26 September 2013 00:50
To: List about Data Driven Journalism and Open Data in Journalism.
Subject: Re: [ddj] Dynamic backreferences?

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<mailto: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<mailto: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/8452f4f8/attachment-0001.html>


More information about the data-driven-journalism mailing list