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

10
  • 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. Apr 29, 2015 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, 2015 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. Apr 29, 2015 at 4:10
  • Here is a test I did, which did not print out the match: codeshare.io/jLI9l Apr 29, 2015 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, 2015 at 4:14

1 Answer 1

1

To me, a regex is really not the best way to go here, since you need to have some kind of memory to remember the previous number in the first word, for every new line you test.

I'm not saying it can't be done (you have a finite set of numbers, so you could technically enumerate every possible file combination "by hand" with a, very nasty, multi-line regex), what I'm saying is: why using it in the first place, when you can actually write Python code to do it (waaaay faster) ? You could have something like a sorted list of all numbers ["one", ..., "twelve"] and days ["first", ..., "twelfth"], then split your file (as a big str) by lines (making it a list of strs) and then test the lines in a for loop using indices in these lists ?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .