gpa
gpa

Reputation: 2451

String parsing based on mask

I have several string which multiple masks. I would like to know is there any better way of handling strings with mask parsing rather than String.spilt and loop over tokens and identify sequence etc. This code also gets clumsy that lots of token logic have to coded.

Sample masks can be:

  1. PROD-LOC-STATE-CITY
  2. PROD-DEST-STATE-ZIP
  3. PROD-OZIP-DZIP-VER-INS

Sample Strings:

  1. CoolDuo-GROUND-NYC-10082

Sample code:

 String[] arr = input.split("-");
 int pos = 0;
 for(String k:arr){

  if(pos == 0) {
     //-- k is of PROD
     ...
     ...
  }
  ..
  ...

  pos++;
}

Above type of code is kept for every mask type.

Upvotes: 0

Views: 1351

Answers (2)

Michael Cheremuhin
Michael Cheremuhin

Reputation: 1383

You can use regex groups to get target strings by group names http://docs.oracle.com/javase/tutorial/essential/regex/groups.html. Check this Regex Named Groups in Java

If you can't use named groups, you can do it in this way (if your are absolutely sure in your strings structure):

final static int PROD_POS = 1;
final static int STATE_POS = 3;

...

Pattern pattern = Pattern.compile("(some_regexp)-(some_regexp)-(some_regexp)");
Matcher matcher = pattern.matcher(input);

if ( matcher.matches() ) {
    String state = matcher.group(STATE_POS);
}

Upvotes: 7

StevenTsooo
StevenTsooo

Reputation: 508

If you really want to delve in quite deep into this problem when your masks gets quite too big to manage, you can use some sort of lexical analysis packages available to java.

If you want to get a basis of what that really means look here (http://en.wikipedia.org/wiki/Lexical_analysis)

A popular package out there for java is JFlex (http://jflex.de/), but there are many others out there, just Google it for best results!

Best of luck

Upvotes: 0

Related Questions