我正在尝试匹配以自动评分学生对正确答案为的问题的答案:
read and execute for owner and read for everyone
他们的答案顺序无关紧要
read for everyone and read and execute for owner
是可以接受的答案。然后所有的绒毛(和,为)并不重要所以我真的只是寻找这些
read execute owner read everyone
read everyone read execute owner
我可以让正则表达式接受任何一个答案
(?=.*read.*execute.*owner)(?=.*read.*everyone)
但显然,接受更多明显错误的答案,例如“读取执行所有者读取执行所有人”。所以我尝试使用负面预测与所有人“执行”,但它仍然匹配所有者的“执行”并且说没有正则表达式匹配。
有没有办法完成我想要做的事情?感谢。
答案 0 :(得分:1)
只需和/和可选。
# (?=.*(\bread(?:\s+and)?\s+execute(?:\s+for)?\s+owner\b))(?=.*(\bread(?:\s+for)?\s+everyone\b))
(?=
.*
( # (1 start)
\b read
(?: \s+ and )?
\s+ execute
(?: \s+ for )?
\s+
owner \b
) # (1 end)
)
(?=
.*
( # (2 start)
\b read
(?: \s+ for )?
\s+ everyone \b
) # (2 end)
)
编辑:您还可以选择之间的任何字词 通过从关键字之间排除所有关键字来获得关键词 像这样 -
# (?=.*(\bread(?:\s+(?:(?!\b(?:read|execute|owner|everyone)\b).)+?)?\s+execute(?:\s+(?:(?!\b(?:read|execute|owner|everyone)\b).)+?)?\s+owner\b))(?=.*(\bread(?:\s+(?:(?!\b(?:read|everyone|execute|owner)\b).)+?)?\s+everyone\b))
(?=
.*
( # (1 start)
\b read
(?:
# Optional words Between Keywords -
# not any of this or the other ones keywords
\s+
(?:
(?!
\b
(?:
read # this
| execute # this
| owner # this
| everyone # other
)
\b
)
.
)+?
)?
\s+ execute
(?:
# Optional words Between Keywords
\s+
(?:
(?!
\b
(?:
read
| execute
| owner
| everyone
)
\b
)
.
)+?
)?
\s+
owner \b
) # (1 end)
)
(?=
.*
( # (2 start)
\b read
(?:
# Optional words Between Keywords -
# not any of this or the other ones keywords
\s+
(?:
(?!
\b
(?:
read # this
| everyone # this
| execute # other
| owner # other
)
\b
)
.
)+?
)?
\s+ everyone \b
) # (2 end)
)