[regex] How is the AND/OR operator represented as in Regular Expressions?

I'm currently programming a vocabulary algorithm that checks if a user has typed in the word correctly. I have the following situation: The correct solution for the word would be "part1, part2". The user should be able to enter either "part1" (answer 1), "part2" (answer 2) or "part1, part2" (answer 3). I now try to match the string given by the user with the following, automatically created, regex expression:

^(part1|part2)$

This only returns answer 1 and 2 as correct while answer 3 would be wrong. I'm now wondering whether there's an operator similar to | that says and/or instead of either...or.

May anyone help me solve this problem?

This question is related to regex operators

The answer is


'^(part1|part2|part1,part2)$'

does it work?


Does this work without alternation?

^((part)1(, \22)?)?(part2)?$

or why not this?

^((part)1(, (\22))?)?(\4)?$

The first works for all conditions the second for all but part2(using GNU sed 4.1.5)


use
if in vim:

:s/{\|}/"/g

will replace { and } on " so {lol} becomes "lol"


Not an expert in regex, but you can do ^((part1|part2)|(part1, part2))$. In words: "part 1 or part2 or both"


Or you can use this:

^(?:part[12]|(part)1,\12)$