I have an input that needs to be validated and I was looking at a way to use a regex to do it.
Input Syntax
add passenger general {passenger-name} add passenger airline
{passenger-name} add passenger loyalty {passenger-name} {current-
loyalty-points} {using-loyalty-points} {has-extra-bag}
Input
add passenger airline Trevor add passenger general Mark add passenger
loyalty Joan 100 FALSE TRUE add passenger general Daniel
Few points
- There can be any number of add passenger statements in the input
- Passengers are of 3 types, general or airline or loyalty
- For general passengers it would be add passenger general Mark
- For airline passengers it would be add passenger airline Trevor
- For loyalty passengers it would be add passenger loyalty Joan 100 FALSE TRUE
I had tried the below logic but it does not validate the multiple occurrence of the sentences. Like it validates the first occurrence of add passenger general but will not for any subsequent add passenger general occurrences. Similar for any other type of passengers
String s = "add passenger general Mohit add passenger general ra add
passenger airline rajui";
Pattern pattern = Pattern.compile("((add passenger general) \\D+)*
((add passenger airline) \\D+)*");
Matcher matcher = pattern.matcher(s);
System.out.println(matcher.matches());