The Regular Expression
widget receives data from another widget, runs a regular expression on the data to find and replace one or more characters, and then publishes the converted data for other widgets to use.
For more information about regular expressions, see this cheat sheet
. Note that this Web site is not maintained by IBM.
A regular expression
defines a search pattern for finding and replacing characters in strings. A regular expression (often abbreviated as regex
) uses syntax that allows programs to parse strings of text and match one or more characters based on the search pattern.
To understand the required syntax, let’s start with an example of how you can use a regular expression to remove portions of a string. Take the following string:
Snow Advisory - Hamilton (New York)
You can write the following syntax to extract the city name Hamilton:
Basically, the regular expression finds and removes all the characters between the dash and the open parenthesis that starts the name of the state. We can break the syntax into pieces so you can understand how it works. The rows in the table describe the syntax in the order in which they appear in the regular expression.
Every regular expression starts with a backward slash. This tells the string parser to start the search pattern.
Because the dash immediately follows the slash that starts the search pattern, the string parser locates the first dash in the string.
Now that you have located the dash, you need to locate the white space that immediately follows the dash. In regular expression syntax, white space is denoted by the special character ’s’. Similar to HTML special characters, ’s’ is just one of several characters in regular expression syntax that have special meaning. In regular expression syntax, whenever you use ’s’ to denote a white space, you need to escape it with a backward slash so that the string parser knows to look for the white space and not the actual character ’s’.
This component of the syntax tells the string parser to locate and group all characters that follow the dash and the white space. In regular expression syntax, parentheses tell the string parser to group the results of the pattern so that you can reference the group later. In this example, the .* syntax tells the parser not to exclude any characters in the results. In other words, the string parser will locate any character that follows the dash and white space.
You learned about the special character ’s’ and backward slash earlier in this table. In this part of your regular expression, you are telling the string parser to locate the white space that immediately follows the group of characters that follow a dash and a white space.
This is the last backward slash in the regular expression. This tells the string parser to stop the search pattern.
Ends the regular expression
You can configure the Regular Expression widget to do any of the following:
- Replace variables in a string with content being published by a feed
- Use a regular expression to match and replace variables in string
See the following table to learn which events the Regular Expression widget can send and receive:
Content as Any Data -- event of type any that sends content that has been transformed by applying a regular expression to content received from another widget
Content using Any Data -- event of type any that receives content from another widget
Goal: Use the Regular Expression widget to manipulate data that is being passed from one widget to another widget on the page. In this example, we pass a city name from the Customer List demo widget, use the Regular Expression widget to replace the city name with a different one, and then pass the city name to the Event Explorer widget to display the city name.
- Drag the Customer List, Regular Expression, and Event Explorer widgets onto the page.
- Hover you cursor over the Regular Expression and click the menu options icon . Click Edit Settings. In the configuration window, do the following steps:
- Select Use regular expression to match and replace variables.
- In the Regular expression field, type Brooksville.
- In the Replacement variable field, type Greenville.
- Click Save to close the configuration window.
- Hover your cursor over the Customer List widget, and click the wiring icon to open the wiring interface. Do the following steps:
- In the Select content to send section, select City as Text.
- In the Select a widget to receive content section, select Regular Expression.
- In the Select an action section, select Content as Any Data.
- Click Done.
- Hover your cursor over the Regular Expression widget, and click the wiring icon to open the wiring interface. Do the following steps:
- In the Select content to send section, select Content as Any Data.
- In the Select a widget to receive content section, select Event Explorer.
- In the Select an action section, select Event data using Any Data.
- Click Done.
- In the Customer List widget, start clicking various rows. Notice how the Event Explorer widget displays the city names. Also notice that it always changes Brookville to Greenville as a result of the replacement settings that you configured above.