如何比较两个带有可选字符的字符串?

时间:2019-05-18 15:17:11

标签: python regex django string-comparison regex-greedy

考虑一下我们有一个函数(在Django || python中),它比较两个字符串,一个是正确答案,另一个是学生答案字符串。

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

我想用正确的字符串检查student_answered,但是括号是可选的,这意味着以下所有Student_answered都是正确的:

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

注意:对于所有问题,我们的格式都不相同,这意味着括号的位置不同,例如,也许只有一个或多个括号。

2 个答案:

答案 0 :(得分:2)

也许,在这里我们可以忽略并查看所需的输出:

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

RegEx

如果这不是您想要的表达式,则可以在regex101.com中修改/更改表达式。

RegEx电路

您还可以在jex.im中可视化您的表达式:

enter image description here

JavaScript演示

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测试

# 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

如果您希望从字面上检查正确答案,则可能需要逐字检查。也许此表达式会起作用:

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

enter image description here

Python代码

您可以简单地添加一个if进行匹配,但不匹配:

# -*- 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!')

输出

YAAAY! "an apple device" is a match 

完全匹配和分组的JavaScript演示

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}`);
    });
}

然后,您可以将所需的其他任何字符添加到列表中,例如括号:

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

enter image description here

这还将传递一些拼写错误的单词,我想这是所希望的。如果没有,您可以简单地删除[]并使用具有逻辑OR的捕获组:

(apple|orange|banana)?

答案 1 :(得分:0)

"apple" in student_answer #matches 'papple'

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

此外,您可以替换常见的添加剂。

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