0

I am trying to come up with the right regex (compatible with python) that would match a file that contained sample input as follows:

On the third day of Christmas
 my true love sent to me:
 three worms a eating
 two garlic cloves
 and a koala in a bamboo spree

This is just ONE possibility. The important part is that the first line have "On the X day of Christmas" with no wrong capitalization (like ChrISTmas).

That should be something like

\^On the [first|second|third|fourth]... 

all the way up to twelfth. Same thing with the ending, last line just needs to have 'and a ...'

 $and a *\         (?)

But how can I check with regex that each line of the file is in the correct order?

For example, 'two garlic...' before 'three worms' is NOT correct

 On the third day of Christmas
 my true love sent to me:
 two garlic cloves         #SWITCHED
 three worms a eating
 and a koala in a bamboo spree

Similarly, it can't jump bits. If it starts with 'five X', the next line needs to be 'four Y', 'three', 'two', etc.

 On the third day of Christmas
 my true love sent to me:
 five cats a dancing       #Goes from 'five cats' to 'two garlic', not ok
 two garlic cloves
 and a koala in a bamboo spree

Here are some attempts at matching multi-line regex I came up with, but to no avail. http://www.codeshare.io/jLI9l

Here's another one where I removed the file-input and tried something else: http://www.codeshare.io/u4E7t

I want it to be able to match and print 'correct' if the input file structure/match is correct

  • So the correct versions are stored somewhere already? If so, once you match, diff the results. ss64.com/bash/diff.html Note the example at the very bottom. – cremefraiche Apr 29 '15 at 2:15
  • Your requirements are not clear. Is there a relation of the word "third" in line 1 to the word "three" in line three? Is a sequence of lines containing, e.g., "eighth", "...", "four", "two", "and a", a valid match, or is the possible word set in the last three lines generally only from the words "three", "two", "and a"? – Janis Apr 29 '15 at 4:04
  • @Janis sorry, to be more clear, the input file only is seen as a 'correct' file if it starts off with On the X day of Christmas (^On the [first|second| etc.) and then if the LAST of the file begins with 'and a'. I'm having a hard time with python checking multiline regex and printing a match for that. – user3295674 Apr 29 '15 at 4:10
  • Here is a test I did, which did not print out the match: codeshare.io/jLI9l – user3295674 Apr 29 '15 at 4:13
  • So far it was clear; I got confused (and still am) by your requirement "if lines 2 and 3 were switched [...]" implies "incorrect". – Janis Apr 29 '15 at 4:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.