Regular Expression (regex) is a special text string that extracts a particular dynamic parameter from the buyer API response. Each Regular Expression field on Step 3 should be filled in with the appropriate expression so that all success and failure responses from buyer API are mapped properly.
Basic Rules
Regular expressions are formed according to the same rules for all formats (HTTP, XML and JSON). The most often used regular expression in API Builder looks like (?<Value>[^&]*) in HTTP, (?<Value>[^<]*) in XML, or (?<Value>[^,]*) in JSON. This regex consists of the following elements:
- ?<Value>* is an expression used in any API Builder regex, where <Value> is a standard part, which corresponds to the necessary value from the response code.
- [^&], [^<], or [^,] mean that only the part before the corresponding symbol should be extracted (& in HTTP, < in XML, or comma in JSON). Check the example code in Buyer Specification to find the symbol after the necessary value, and specify it in brackets.
Characters with special meanings in regular expressions are described in Regular Expression FAQ on Step 3:
The Basic Flow
1. Find a response code example in buyer specification.
2. Check the format of the necessary value.
3. Enter the appropriate regular expression to Regular Expression field on Step 3.
4. Past the code example to the Lead Response field to test your regex. If the necessary part of the response is highlighted, it means that it is successfully extracted.
Regular Expression Templates in HTTP
(?<Value>[^&]*) is the most often used regex in HTTP. Usually Payout, Response ID and Error regular expressions look similarly for one Buyer API:
- Payout=(?<Value>[^&]*)
- Lead_ID=(?<Value>[^&]*)
In general, Response ID is any additional ID sent by buyer API, which is different from Ping ID. Response ID is most often called Lead ID in the buyer response. Nevertheless, it can have a different name.
- Error=(?<Value>[^&]*)
- Ping_ID=(?<PingId>[^&]*)
In the regular expression for Ping ID, <PingId> is used instead of <Value> because it is used in the Template on Step 2 with the same name.
Regular Expression Templates in XML and SOAP
(?<Value>[^<]*) is the most often used regex in XML and SOAP. Usually Payout, Response ID and Error regular expressions look similarly for one buyer API:
- <Payout>(?<Value>[^<]*)</Payout>
- <Lead_ID>(?<Value>[^<]*)</Lead_ID>
In general, Response ID is any additional ID sent by buyer API, which is different from Ping ID. Response ID is most often called Lead ID in the buyer response. Nevertheless, it can have a different name.
- <Error>(?<Value>[^<]*)</Error>
- <Ping_ID>(? <PingId>[^<]*)</Ping_ID>
In the regular expression for Ping ID, <PingId> is used instead of <Value> because it is used in the Template on Step 2 with the same name.
Regular Expression Templates in JSON
Basic rules described in the beginning of this document work for JSON as well. However, JSON code often contains spaces, which must be replaced by \s in regular expressions.
Sometimes buyer specification contains JSON code example without spaces, while API response includes them. It is recommended to perform a complete testing procedure on Step 4 to check whether regular expression works correctly.
(?<Value>[^,]*) is the most often used regex in JSON. Usually Payout, Response ID and Error regular expressions look similarly for one buyer API:
- "Payout":(?<Value>[^,]*)
- "Lead_ID":"(?<Value>[^,]*)"
In general, Response ID is any additional ID sent by buyer API, which is different from Ping ID. Response ID is most often called Lead ID in the buyer response. Nevertheless, it can have a different name.
- "Error":"(?<Value>[^,]*)”
- "Ping_ID":"(?<PingId>[^,]*)"
In the regular expression for Ping ID, <PingId> is used instead of <Value> because it is used in the Template on Step 2 with the same name.
Complex JSON Regex Examples
1. When there are several spaces in JSON response, it is recommended to include \s*:\s* into the regular expression.
Code example: "response": {""errors": false, "message" : "Success""}
Regex: "message"\s*:\s*"(?<Value>[^""]*)
2. If the value contains other characters except digits (text or _ symbol), there are two alternative ways to form the regex:
1) specify this format in the regex (this is useful when there is only one such parameter in the response; in this case, regex will extract only value in specified format):
Code example: "ping_id":"1342b4b1_test"
Regex: "ping_id":"(?<PingId>[\w]*)
2) specify the symbol, before which the regex should stop when extracting the value:
Code example: "ping_id":"1342b4b1_test"
Regex: (?<Value>[^""]*)
3. In case when the buyer can put the value in one of several possible places of the response, specify all possible symbols, before which the regex should stop when extracting the value (use [^,|""|}] expression to take into account comma, “” or } symbol as a stop, and ([\s]*) expression if the number of spaces before the value is unknown):
Code example: "bid": 3.0,
or {"bid": 3.0}
Regex: "bid":([\s]*)(?<Value>[^,|""|}]*)
0 comments
Please sign in to leave a comment.