# Returning values based on regular expressions matches

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. Place the input contact element on the corresponding output to map the child elements.

   ![Input element for output map.](https://help.qlik.com/talend/en-US/data-mapper-functions-reference-guide/Resources/images/match_values_with_regex_01_map.png)
2. Place an IfThenElse function on the output status attribute.
3. Place an And function on the Condition attribute, then place two Matches function on the And function.

   ![Placement of functions.](https://help.qlik.com/talend/en-US/data-mapper-functions-reference-guide/Resources/images/match_values_with_regex_02_ifthenelse.png)
4. Place 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. Place a Constant function on the Then argument, double-click it and enter valid in the Value field, then click OK.
8. Place 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. ![Map result.](https://help.qlik.com/talend/en-US/data-mapper-functions-reference-guide/Resources/images/match_values_with_regex_03_result.png)

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>
```