1

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());
3
  • 3
    Welcome to Stack Overflow! Unfortunately your question only contains requirements - it is not showing any efforts from your side to solve this problem yourself. Please add your attempts to this questions - as this site is not a free "we do your (home)work" service. Beyond that: please turn to the help center to learn how/what to ask here. Thanks! Commented Jun 25, 2018 at 14:44
  • What have you done so far ? What research ? What is causing you difficulties/problems ? Why ? What is the code you have so far ? What is the output wanted ? Etc... Commented Jun 25, 2018 at 14:47
  • @vincrichaud Thanks for the response. I have added additoinal details on what I had tried. I was unable to achieve the desired output. Commented Jun 25, 2018 at 14:50

2 Answers 2

1

Here is a regex that will validate the entire input string, or,
if the outter cluster group is taken off, you could get individual add passanger
types. The types are indicated based on if capture group 1, 2, or 3 matched and
is the name.

"(?i)^(?:\\s*add\\s+passenger\\s+(?:general\\s+(\\w+)|airline\\s+(\\w+)|loyalty\\s+(\\w+)\\s+(\\d+)\\s+(TRUE|FALSE)\\s+(TRUE|FALSE)))+\\s*$"

Readable version

 (?i)
 ^    
 (?:
      \s* add \s+ passenger \s+ 
      (?:
           general \s+ 
           ( \w+ )                       # (1)

        |  airline \s+ 
           ( \w+ )                       # (2)

        |  loyalty \s+ 
           ( \w+ )                       # (3)
           \s+ 
           ( \d+ )                       # (4)
           \s+ 
           ( TRUE | FALSE )              # (5)
           \s+ 
           ( TRUE | FALSE )              # (6)
      )
 )+
 \s* 
 $

Output

 **  Grp 0 -  ( pos 0 : len 128 ) 
add passenger airline Trevor add passenger general Mark add passenger 
loyalty Joan 100 FALSE TRUE add passenger general Daniel  
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @sln. Works great.
0

This isn't an answer to your question; rather, it is some advice. Yes, it would be possible to create one or more regular expressions to parse your input. However, given all the requirements, it would end up being fairly complex. Once it was written, any change to your inputs would require modifying those regular expressions.

First, I suggest, if possible, changing your input. Something like JSON would be much easier to create and parse.

Second, sometimes it is easier to start at the beginning of your String and break it down using String.indexOf(), .substring(), etc. This lets you control the logic in a more maintainable manner.

2 Comments

Thanks Steve. I know that JSON would have been better, but I was parsing input from an external source file. The input is currently out of my control and hence I was using the regex. I can break up the input and validate individual add passenger statements but was thinking if there is some way if I could validate the entire input
I hear you. That's why developers get paid big $$$!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.