Returning values based on regular expressions matches - 8.0

Talend Data Mapper Functions Reference Guide

Version
8.0
Language
English
Product
Talend Big Data Platform
Talend Data Fabric
Talend Data Management Platform
Talend Data Services Platform
Talend MDM Platform
Talend Real-Time Big Data Platform
Module
Talend Studio
Content
Design and Development > Designing Jobs
Last publication date
2023-10-26

Use an IfThenElse function with a Matches function to define an output value depending on the pattern of an input value.

Before you begin

You have created a map with an input and an output structure. You can use the samples provided below.

About this task

In this example, you have an XML file containing contact information. You want to check that the state and phone elements match a specific pattern and indicate whether a contact is valid or invalid using an attribute. You can use the following XML sample as input:
<contacts>
	<contact>
		<lastName>Harrison</lastName>
		<firstName>Jane</firstName>
		<street>French Camp Turnpike Road</street>
		<zipCode>30316</zipCode>
		<city>Atlanta</city>
		<state>Georgia</state>
		<phone>(678)123-4567</phone>
	</contact>
	<contact>
		<lastName>Johnson</lastName>
		<firstName>Katherine</firstName>
		<street>Santa Rosa South</street>
		<zipCode>85162</zipCode>
		<city>Phoenix</city>
		<state>AZ</state>
		<phone>(602)789-0123</phone>
	</contact>
	<contact>
		<lastName>Monroe</lastName>
		<firstName>Martin</firstName>
		<street>N Kentwood</street>
		<zipCode>32315</zipCode>
		<city>Tallahassee</city>
		<state>FL</state>
		<phone>(805) 456-7890</phone>
	</contact>
</contacts>
You can use the following XML sample to create your output structure:
<contacts>
	<contact status="">
		<lastName/>
		<firstName/>
		<street/>
		<zipCode/>
		<city/>
		<state/>
		<phone/>
	</contact>
	<contact status="">
		<lastName/>
		<firstName/>
		<street/>
		<zipCode/>
		<city/>
		<state/>
		<phone/>
	</contact>
</contacts>

Procedure

  1. Drag and drop the input contact element on the corresponding output to map the child elements.
  2. Drag and drop an IfThenElse function on the output status attribute.
  3. Drag and drop an And function on the Condition attribute, then drag and drop two Matches function on the And function.
  4. Drag and drop the input state element on the Input Value argument of the first Matches function and the input phone element on the second one.
  5. Double-click the Matches function associated with the state element, then enter ^[A-Z]{2}$ in the Regex Value field, select the Case sensitive check box and click OK.
    This indicates that the value of the state element should be two uppercase letters.
  6. Double-click the Matches function associated with the phone element, then enter ^\(\d{3}\)\d{3}-\d{4}$ in the Regex Value field and click OK.
    This indicates that the value of the phone element should be three digits in parentheses followed by three digits, a hyphen and four digits.
  7. Drag and drop a Constant function on the Then argument, double-click it and enter valid in the Value field, then click OK.
  8. Drag and drop a Constant function on the Else argument and set its value to invalid, then click OK.

Results

Your map is configured, the status attribute of the contact element is valid if both the state and phone number match the patterns defined. Otherwise the value is invalid.
You can use the Test Run feature to see the result. In this example, the following output is returned:
<contacts>
  <contact status="invalid">
    <lastName>Harrison</lastName>
    <firstName>Jane</firstName>
    <street>French Camp Turnpike Road</street>
    <zipCode>30316</zipCode>
    <city>Atlanta</city>
    <state>Georgia</state>
    <phone>(678)123-4567</phone>
  </contact>
  <contact status="valid">
    <lastName>Johnson</lastName>
    <firstName>Katherine</firstName>
    <street>Santa Rosa South</street>
    <zipCode>85162</zipCode>
    <city>Phoenix</city>
    <state>AZ</state>
    <phone>(602)789-0123</phone>
  </contact>
  <contact status="invalid">
    <lastName>Monroe</lastName>
    <firstName>Martin</firstName>
    <street>N Kentwood</street>
    <zipCode>32315</zipCode>
    <city>Tallahassee</city>
    <state>FL</state>
    <phone>(805) 456-7890</phone>
  </contact>
</contacts>