2

I am working on writing test cases for student problems on replit. I don't want students to have to match my output exactly for their problems, but their solutions do need to contain the correct answer. For example, here is a simple program that asks students to input a number, a mass and then a date and outputs some results.

Please enter a number between 1 and 10 (inclusive): 8
The Roman number for 8 is VIII.
Please enter the mass in kg: 90
The weight of 882.0000000000001 is just right.
Please enter the 2 digit month: 06
Please enter the 2 digit date: 10
Please enter the 2 digit year: 60
The date 6/10/60 is magic

I would like for the students to have their output have any text they choose as long as they have the correct answers (IX, 578.2 and "is magic") for this particular set of data.

I need to use regex test for this however, I have not been successful in writing a regex that works.

Note My regex experience is limited. I used chatGPT to get started. I then put the output from chatGPT into regex101.com and used the debugger so I could follow the steps the parser (?) was taking to move through my answer vs. the expression.

Here are my various attempts (I modified these serval times as well but you get the gist).

  1. [a-zA-Z0-9:\.\(\)]+VIII[\n\.:a-zA-Z0-9]+882.0000000000001+[\n\.:a-zA-Z0-9]+is magic[\n\.a-zA-Z0-9]

  2. \b\w+(?:\s+\w+)*\s+VIII\b[\w\s\d,:.]+882[\w\s\d,:.]+\bis magic\b

  3. \b\w+(?:\s+\w+)*\s+VIII\b\w+[?:\w+\s+\d,:.]+882.0000000000001\b\w+[\w+\s+\d+,\/:.]+\bis magic\b

  4. .*?VIII\n.*?882\n.*?is magic\n

None of the above regexes nor their variations worked with the output in bold above. One of my guesses is that the \n is causing problems but I couldn't prove or disprove that theory....

Thanks much in advance.

InSync
  • 4,851
  • 4
  • 8
  • 30
Ann Root
  • 21
  • 1
  • You're looking for a pattern that will match the entire text, or just one result at a time? – Reilas Jun 14 '23 at 23:15
  • It took me a while to understand your problem: Your students make programs (probably in Python) that are supposed to do Roman numeral conversion, weight conversion and calculation, but those programs output in different ways and now you need some regexes (perhaps also in Python) to match and verify the result, correct? – InSync Jun 14 '23 at 23:16
  • Your best bet is to try to find the lines you need before matching the regexes against them. For example: Match [`(?i)\b(?:V?I{1,3}|I?[VX])\b`](https://regex101.com/r/YidxQF/2) against the first two lines (this matches all 10 Roman numbers, case-insensitively); if it matches, extract the match and verify the result; otherwise, the result is incorrect. This is necessary since there is nothing that prevents your student from adding something like "*This program was made by VI*" at the top, which may incidentally matches the regex at the same time your test happens to be `6`. – InSync Jun 14 '23 at 23:28
  • Your *main* problem is asking/teaching students to create programs that do more than one thing, which as per [SOLID](https://en.wikipedia.org/wiki/SOLID) is **bad** design - the "S" stands for [Single responsibility](https://en.wikipedia.org/wiki/Single-responsibility_principle). One of the negative impacts of violating this long and well understood principle is it's hard to test. Instead, have your students write several smaller programs that output simple (ie free of chat chat text) values and test that - eg output just `VIII`, not `The Roman number for 8 is VIII.`. – Bohemian Jun 14 '23 at 23:57

1 Answers1

-1

To check for those 3 values, within the entire text, you can use the following.

(?s).*?IX.*?578\.2.*is magic.*

"... One of my guesses is that the \n is causing problems ..."

The (?s) modifier will toggle-on, "single-line" mode.  This will cause the dot meta-character, ., to match new-line delimiters, as well.

Reilas
  • 3,297
  • 2
  • 4
  • 17