Java – regular expressions X and / or y
Consider the following regular expressions, where x and y are any regular expressions
XY|YX|X|Y
This regular expression tests X and / or Y, but if the and clause is not found, you must carefully examine the expression
What is the best way to match X and / or Y?
Solution
Regular expression / XY| YX?/ It should match the four situations you listed But this is limited. Just like the regular expressions you use have a common match, you may get unexpected results
If this is a problem, more requirements may be added to the problem
explain:
The first half will try to match the X regular expression, and then try to match the Y regular expression 0 or 1 times If the X regular expression fails, it will try the second half; Will try to match the Y regular expression, and then will try to match the X regular expression 0 or 1 times