-3

consider we have a function (in Django || python) which compares two strings, one is the correct answer and another is student answered string.

correct = '(an) apple (device)'
student_answerd = 'apple device'

I want to check student_answered with the correct string but parentheses are optional it means all the below student_answered is correct:

    case 1: an apple device
    case 2: an apple
    case 3: apple device
    case 4: apple

notice: we don't have the same correct format for all questions it means the location of parentheses is different for example maybe we just have one parentheses or more.

Emma
  • 27,428
  • 11
  • 44
  • 69
Arash
  • 281
  • 3
  • 12

2 Answers2

2

Maybe, here we could ignore ( and ) and see the desired output:

(.*?)(?:[()]|)

RegEx

If this wasn't your desired expression, you can modify/change your expressions in regex101.com.

RegEx Circuit

You can also visualize your expressions in jex.im:

enter image description here

JavaScript Demo

const regex = /(.*?)(?:[()]|)/gm;
const str = `(an) apple (device)
apple device`;
const subst = `$1`;

// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);

console.log('Substitution result: ', result);

Python Test

# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility

import re

regex = r"(.*?)(?:[()]|)"

test_str = ("(an) apple (device)\n"
    "apple device")

subst = "\\1"

# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)

if result:
    print (result)

# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

RegEx

If you wish to literally check for correct answers, you might want to do it word by word. Maybe, this expression would work:

^((an|one)?(\s?)([aple]+)?(\s?)([devic]+)?)$

enter image description here

Python Code

You can simply add an if for match and not match:

# -*- coding: UTF-8 -*-
import re

string = "an apple device"
expression = r'^((an|one)?(\s?)([aple]+)?(\s?)([devic]+)?)$'
match = re.search(expression, string)
if match:
    print("YAAAY! \"" + match.group(1) + "\" is a match  ")
else: 
    print(' Sorry! No matches!')

Output

YAAAY! "an apple device" is a match 

JavaScript Demo for Full Match and Groups

const regex = /^((an|one)?(\s?)([aple]+)?(\s?)([devic]+)?)$/gm;
const str = `an apple device
an apple
apple device
apple
one apple
one appl device
two apple deive
on apple device
a apple device`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

Then, you can add any other chars that you wish to the list, such as parentheses:

^(((one|an)+)?(\s?)([aple()]+)?(\s?)([()devic]+)?)$ 

enter image description here

This would also pass some misspelled words, which I 'm guessing that would be desired. If not, you can simply remove [] and use capturing groups with logical ORs:

(apple|orange|banana)?
Emma
  • 27,428
  • 11
  • 44
  • 69
  • 1
    That's good but I need some codes to return me true or false, for example, if a student writes "Two Apple Devices" I want to get false compare result. notice: all characters are already converted to .lower() – Arash May 18 '19 at 15:39
  • 1
    Miss understanding buddy forget the last sample, consider we have a string like (a) hardhat(s) parentheses are optional it means: a hardhats is OK hardhats is OK a hardhat is OK and so on ... the parentheses are optional and remain is forced. – Arash May 18 '19 at 16:19
0
"apple" in student_answer #matches 'papple'

or

"apple" in student_answer.split() # doesn't match 'papple'

Additionally, you can replace common additives.

a = [',', 'an', ' ', 'device']
for i in a:
    student_answer = student_answer.replace(a, '')
student_answer == 'apple'

Benoît P
  • 3,179
  • 13
  • 31