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

Announcements
Write Table now available in Qlik Cloud Analytics: Read Blog
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

Regular Expression in Java tMap

Hello All,
I am looking for a way to use Regular Expression inside tMap, so that I can use parse my log file which has schema which is dynamic
--loc=us --tool=env --binary=/i686-pc-linux2.4/bin/env
--loc=us --binary=/i686-pc-linux2.4/bin/rm --tool=rm
--binary=/i686-pc-linux2.4/bin/grep --loc=us --tool=grep
--loc=us --tool=mv
I tried to understand how to extract text using Regular Expression inside java tMap expression editor. How can I call "--loc=(.+?) " inside tMap considering I have the log defined as delimitedfile.
Thanks in advance for your help.
Regards,
Sathish
Labels (3)
4 Replies
Anonymous
Not applicable
Author

Based on the following post, I should be created custom routine it seems.
https://community.talend.com/t5/Design-and-Development/resolved-How-to-extract-substrings-according-...
Anonymous
Not applicable
Author

Hello All,
I had to write the following routine
package routines;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MyRegExp {
public static String loc(String str) {
Pattern p = Pattern.compile("(?-m)--loc=(.+?)(\\s|$)");
Matcher m = p.matcher(str);
if (m.find()) {
return m.group(1);
} else {
return null;
}
}

public static String tool(String str) {
Pattern p = Pattern.compile("(?-m)--tool=(.+?)(\\s|$)");
Matcher m = p.matcher(str);
if (m.find()) {
return m.group(1);
} else {
return null;
}
}
public static String binary(String str) {
Pattern p = Pattern.compile("(?-m)--binary=(.+?)(\\s|$)");
Matcher m = p.matcher(str);
if (m.find()) {
return m.group(1);
} else {
return null;
}
}
}

Please let me know if know a better way.
Regards,
Sathish
alevy
Specialist
Specialist

You could do it directly in a tMap expression but your way is, I think, better e.g.
row1.columnName.matches("(^|.*\\s)--foo=+(\\s.*|$)")?row1.columnName.replaceAll("(^|.*\\s)--foo=(+)(\\s.*|$)","$2"):null
_AnonymousUser
Specialist III
Specialist III

Je vous laisse découvrir un aperçu du reportage et de cette séance photo de couple, et je remercie encore Antonina et Aurélien pour leur sympathie et Boutique de mariage en ligne la grande confiance qu?ils m?ont accordé !
Le loft à Coubevoie - 24, rue Pierre Brossolette 92250 robemariageonline.com La Garenne Colombes/ Courbevoie - www.leloft.fr/Leloft/leloft-location-accueil.html
Les bonnes adresses du mariage :