Talend Equivalent for MatchField Function in Datastage

 
package routines;

import java.util.regex.*;

public class UserString {

	public static String MatchField(String sourceStr, String patternStr, Integer fieldNo) {
		String matchedStr = "";
		
		if (fieldNo == null) {
			throw new UnsupportedOperationException("fieldNo is supposed to be NOT NULL!!");
		}
		if (patternStr == null) {
			return "";
		}
		if (sourceStr == null) {
			return "";
		}
		
		patternStr = formatPatternDStoTld(patternStr);
		Pattern pattern = Pattern.compile(patternStr);
		Matcher matcher = pattern.matcher(sourceStr);
		if (matcher.find()) {        			
			matchedStr = matcher.group(fieldNo);
		} else {		
			matchedStr = "";
		}
		return matchedStr;
	}
	
	
	public static String formatPatternDStoTld(String input) {
		StringBuilder output = new StringBuilder();
		String outputStr = "";
		for (int i = 0 ; i < input.length() ; i++) {
			Character firstChar = input.charAt(i);
			if (Character.isDigit(firstChar)) {
				Character secondChar = input.charAt(i + 1);
				int occur = Integer.parseInt("" + firstChar);
				if (Character.isDigit(secondChar)) {
					occur = (occur * 10) + Integer.parseInt("" + secondChar);
					i++;
					secondChar = input.charAt(i + 1);    				
				} 
				if (Character.isLetter(secondChar)) {
					if (secondChar == 'A') {
						output.append("([a-zA-Z]");
					} else if (secondChar == 'N') {
						output.append("(\\d");
					} else if (secondChar == 'X') {
						output.append("([a-zA-Z0-9 ]");
					}
					if (occur == 0) {
						output.append("*)");
					} else {
						output.append("{" + occur + "," + occur + "})");
					}
					i++;
				} else {
					output.append("(" + firstChar + ")");
				}
			} else {
				output.append("(" + firstChar + ")");    			
			}
		}
		outputStr = output.toString();	
		return outputStr;
	}
}

Post a Comment

0 Comments