Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Join us in Bucharest on Sept 18th for Qlik's AI Reality Tour! Register Now
cancel
Showing results for 
Search instead for 
Did you mean: 
janhess
Creator II
Creator II

[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.
Labels (2)
1 Solution

Accepted Solutions
janhess
Creator II
Creator II
Author

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_1 0683p000009MAB6.pngatternmatch.matchPostCode(row1.Z304_DDR_REC_ADDR_2_1).trim() != ""? row1.Z304_DDR_REC_ADDR_2_1 0683p000009MAB6.pngatternmatch.matchPostCode(row1.Z304_DDR_REC_ADDR_3_1).trim() !=""? row1.Z304_DDR_REC_ADDR_3_1 0683p000009MAB6.pngatternmatch.matchPostCode(row1.Z304_DDR_REC_ADDR_4_1).trim() != ""? row1.Z304_DDR_REC_ADDR_4_1 0683p000009MAB6.pngatternmatch.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.

View solution in original post

3 Replies
Anonymous
Not applicable

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
janhess
Creator II
Creator II
Author

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.
janhess
Creator II
Creator II
Author

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_1 0683p000009MAB6.pngatternmatch.matchPostCode(row1.Z304_DDR_REC_ADDR_2_1).trim() != ""? row1.Z304_DDR_REC_ADDR_2_1 0683p000009MAB6.pngatternmatch.matchPostCode(row1.Z304_DDR_REC_ADDR_3_1).trim() !=""? row1.Z304_DDR_REC_ADDR_3_1 0683p000009MAB6.pngatternmatch.matchPostCode(row1.Z304_DDR_REC_ADDR_4_1).trim() != ""? row1.Z304_DDR_REC_ADDR_4_1 0683p000009MAB6.pngatternmatch.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.