One Star

[resolved] pattern matching in tmap

I have an input file with 5 address fields. I need to be able to identify which field contains a uk postcode to map it to an output field called postcode. I downloaded the pattern match Valid UK Post Codes Upper and Lower Case.csv.zip from exchange
"'^ ((?)|((?)|(()|()))) ?{2}$'"
but can't figure out how to use it in a map rule.
Any help greatly appreciated.
1 ACCEPTED SOLUTION

Accepted Solutions
One Star

Re: [resolved] pattern matching in tmap

OK I managed to solve it.
Set up code as follows
import java.util.regex.*;
public class patternmatch {
public static String matchPostCode (String input) {
Pattern postcode = Pattern.compile("^((?)|(?)|(()|(()))) ?{2}$");
Matcher fit = postcode.matcher(input);
if (fit.matches()) {
return input;
} else {
return "";
}
}
}
and used rule like
patternmatch.matchPostCode(row1.Z304_DDR_REC_ADDR_1_1).trim()!=""?null:row1.Z304_DDR_REC_ADDR_1_1
for each of the address lines and
patternmatch.matchPostCode(row1.Z304_DDR_REC_ADDR_1_1).trim()!= ""? row1.Z304_DDR_REC_ADDR_1_1Smiley Tongueatternmatch.matchPostCode(row1.Z304_DDR_REC_ADDR_2_1).trim() != ""? row1.Z304_DDR_REC_ADDR_2_1Smiley Tongueatternmatch.matchPostCode(row1.Z304_DDR_REC_ADDR_3_1).trim() !=""? row1.Z304_DDR_REC_ADDR_3_1Smiley Tongueatternmatch.matchPostCode(row1.Z304_DDR_REC_ADDR_4_1).trim() != ""? row1.Z304_DDR_REC_ADDR_4_1Smiley Tongueatternmatch.matchPostCode(row1.Z304_DDR_REC_ADDR_5_1).trim() != ""? row1.Z304_DDR_REC_ADDR_5_1:row1.Z304_ADDR_REC_ZIP_1
for the zip code.
Seems to work ok.
3 REPLIES
One Star

Re: [resolved] pattern matching in tmap

There is one way to proceed :
Write a routine, with a method that will identify the postcode.
public int identify_postcode(String adress_field_1, String adress_field_2, ... , String adress field_5) {
// there you write the java code to test every field and extract the post code
// convert the postcode to int type
return postcode;
}
Then in the tmap, fill the field postcode with the expression :
routine.identify_postcode(row1.adress1,row1.adress2,...,row1.adress5)
Good luck
One Star

Re: [resolved] pattern matching in tmap

Hi nKxxDV
Thanks for the response. Unfortunately I'm not a Java developer. I had hoped there would be a way in Expression Builder to say something like
if row1.address1 = pattern then row1.address1
else if row1.address2 = pattern then row1.address2
etc.
One Star

Re: [resolved] pattern matching in tmap

OK I managed to solve it.
Set up code as follows
import java.util.regex.*;
public class patternmatch {
public static String matchPostCode (String input) {
Pattern postcode = Pattern.compile("^((?)|(?)|(()|(()))) ?{2}$");
Matcher fit = postcode.matcher(input);
if (fit.matches()) {
return input;
} else {
return "";
}
}
}
and used rule like
patternmatch.matchPostCode(row1.Z304_DDR_REC_ADDR_1_1).trim()!=""?null:row1.Z304_DDR_REC_ADDR_1_1
for each of the address lines and
patternmatch.matchPostCode(row1.Z304_DDR_REC_ADDR_1_1).trim()!= ""? row1.Z304_DDR_REC_ADDR_1_1Smiley Tongueatternmatch.matchPostCode(row1.Z304_DDR_REC_ADDR_2_1).trim() != ""? row1.Z304_DDR_REC_ADDR_2_1Smiley Tongueatternmatch.matchPostCode(row1.Z304_DDR_REC_ADDR_3_1).trim() !=""? row1.Z304_DDR_REC_ADDR_3_1Smiley Tongueatternmatch.matchPostCode(row1.Z304_DDR_REC_ADDR_4_1).trim() != ""? row1.Z304_DDR_REC_ADDR_4_1Smiley Tongueatternmatch.matchPostCode(row1.Z304_DDR_REC_ADDR_5_1).trim() != ""? row1.Z304_DDR_REC_ADDR_5_1:row1.Z304_ADDR_REC_ZIP_1
for the zip code.
Seems to work ok.