Hi everyone, I hope it's appropriate to post this question here. I couldn't find anywhere else to post it (posting to the Ruby group didn't work). I'm trying to write a regular expression to turn this string:
'johnny went to the [shops] and played [games,soccer,foot ball] with others.'
into this:
['johnny went to the', ['shops'], 'and played', ['games', 'soccer', 'foot ball'], 'with others.']
Key features are that white space needs to be stripped from around the snippets, and I need to be able to determine which parts are just normal text and which parts are to be used as gaps where people fill in the missing info. If you haven't already figured it out, this is for a children's quiz engine. When a phrase appears in square brackets it's a blank, the system will display a text box for the user to type into. If there are multiple items in the square brackets seperated by comma's (and optional spaces around the comma's by lazy users) then the system will display the choices in a random order in a drop down box (the first choice in the list is the correct one).
I've had a long go at it myself but I just don't fully grasp the regexp language:
irb(main):027:0> 'johnny went to the [shops] and played [games,soccer,foot ball] with others.'.split(/\[(?:([(?:\s)\w(?:\s)]+)(?:,([(?:\s)\w(?:\s)] +))*)\]/) => ["johnny went to the ", "shops", " and played ", "games", "foot ball", " with others."]
I've tried doing groupings but it seems to make things worse
Any help would be truly appreciated
Cheers,
Brendon