Description
The StringReplacementGen returns a string replacing all sequences of characters matching the regex and replacement string.
Regex or regular expressions define a search pattern (replacement rules) for a constant or referenced string value. If a match is found in the string (based on the defined regex), the Generator will replace it with the specified replacement value. The Generator will remove the matched value from the string if there is no replacement value.
The functionality is similar to 'find and replace' in many modern-day applications. You can replace or remove one or multiple values (e.g., whitespaces, titles, suffixes, individual characters, strings, etc. ).
Example 1 - Replace '1' with Name
value = user1, from Text = \d, to Text = Name
result = userName
Example 2 - Replace 'user' with name
value = user1, fromText = user, toText = name
result = name1
In This Article
- Generator Parameters
- Common Regex Values
- Sample Expressions
- Example 1 - Replace Periods with Hyphens in Phone Numbers
- Example 2 - Remove Extra Space After Area Code in Phone Number
- Example 3 - Remove Titles from Names (Mr., Mrs., Miss., Ms., Dr., Rev.)
- Example 4 - Mask Credit Card Numbers Except for the Last Four Digits
Generator Parameters
The following parameters may be configured for the StringReplacementGen Generator. Items with an asterisk* are required.
- value* - The value to be manipulated. This can be a constant value or a referenced value.
- fromText* - Defines the text that can be a regular expression to which this string is to be matched, or just simple text.
- toText - Defines the value that will replace the found regular expression or simple text specified in the fromText parameter.
Common Regex Values
The table below contains some common RegEx values that can be used independently or to create a more complex regular expression for the StringReplacementGen.
Note: Please see the Wikipedia page on regular expressions to learn more. You can also search in a web browser using the keyword "regex cheat sheet" to see quick reference guides similar to what is shown below.
RegEx Value | Description |
^ | The match must start at the beginning of the string or line in multi-line mode. |
\A | The match must start at the beginning of the string. |
$ | The match must occur at the end of the string or line in multi-line mode. |
\Z | The match must occur at the end of the string. |
\> | Start of a word. |
\< | End of a word. |
. | Wildcard that matches any single character except \n. |
| | Matches one or more specific characters or a group of characters on either side (e.g., c|d represents c or d). |
\ | Used to escape a special character (e.g., \+). |
a | Replaces the character. |
abc | Replaces the matching string. |
* | Replaces 0 or more occurrences of the preceding character. |
? | Replaces 0 or 1 occurrence of the preceding character. |
+ | Matches one or more occurrences of the preceding character. |
\s | Matches any whitespace character. |
\S | Matches any non-whitespace character. |
\w | Matches any word character. |
\W | Matches any non-word character. |
\d | Matches any decimal digit. |
\D | Matches any non-decimal digit. |
\x | Hexadecimal digit. |
\o | Octal digit. |
\b | Matches a backspace. |
\c | Control character. |
\t | Matches a tab. |
\n | Matches a newline. |
\r | Matches a carriage return. |
\v | A vertical tab. |
\e | Match an escape. |
(abc) | Grouping of characters. |
(?:abc) | Non-capturing group of characters. |
[ae] | Matches a character within the specified range. |
[^aei] | Replace all characters that are not in the brackets with another value. |
[a-e] | Replace any character within the specified range. |
[0-7] | Replace any digit within the specified range. |
{5} | Matches the exact number. |
{5,} | Matches 'n' number of occurrences of the preceding character group. |
{5, 10) | Matches everything between the two numbers. |
?= | Lookahead |
?! | Negative lookahead |
?<= | Lookbehind assertion |
?!=, ?<! | Negative lookahead |
?() | Condition (if-then) |
?()| | Condition (if-then-else) |
Sample Expressions
Expression | Description |
^\s+ | Remove all extra white spaces (e.g., User Name becomes User Name). |
^\s+|\s+$|' | Remove white spaces and quotes from the actual value. |
(?:(\.\d*?[1-9]+)|\.)0*$ | Remove trailing zeros from a decimal number (e.g., 100.78000 becomes 100.78). |
\..*$ | Remove decimal and all trailing values (e.g., 100.780000 becomes 100). |
[^\w\s] | Remove all special characters from a string (e.g., User! M. Test@ becomes User M Test). |
Example 1 - Replace Periods with Hyphens in Phone Numbers
A tester wants to replace the periods with hyphens in a US-formatted phone number. In this example, the generated value will be the withHyphens column.
Generators
Two Generators will be used for this example:
- Flexible PhoneNumberGen- Generates US-formatted phone numbers with periods as separators.
- e.g., 766.891.0124
- StringReplacementGen- Replaces the period in each generated phone number with a hyphen.
- e.g., 766-891-0124
FlexiblePhoneNumberGen
Generates a US-formatted phone number. The first generated number will be: 101.001.0100
- Area Code - Starts at 101 and increases by 1 for each record.
- Exchange Code - Starts at 001 and increases by 1 for each record.
- Line Number - Starts at 0100 and increases by 1 for each record.
StringReplacementGen
It will replace all periods in the referenced phone number with hyphens.
- value - References FlexiblePhoneNumberGen
- fromText - Matches each period in the referenced string.
- toText - Replaces matched value with a hyphen.
Example 2 - Remove Extra Space After Area Code in Phone Number
A tester wants to remove the extra space after the area code in phone numbers (e.g., (800) 889-001). This can be done with the StringReplacementGen.
This example uses two Linked Generators:
- ListGen - used in this example to generate the phone numbers with the extra space.
- StringRemoveGen - removes extra space after closing parentheses in phone numbers.
ListGen
Ten phone numbers, each with a space after the closing parenthesis, have been added to the list.
StringRemoveGen
- value - references the ListGen (gen1) for phone numbers
- fromText - \s, remove spaces from string value
Example 3 - Remove Titles from Names (Mr., Mrs., Miss., Ms., Dr., Rev.)
A tester wants to remove the title from each full name so that only each person's first name, middle initial, and last name are included in the generated output.
For this example, two Generators have been linked:
- FullNameGen - generates the full name with a title
- StringReplacementGen - removes the title from each full name
FullNameGen
The prcntSuffix parameter has been set to '0' so the generated names do not contain a suffix.
Note: The FullNameGen can also be set up to generate no titles. In this example, it is being used to generate the required data for the StringReplacementGen example.
StringReplacementGen
Removes the title from each generated value.
- value - references the FullNameGen (gen1)
- fromText - defines the regular expression for the titles that need to be removed
- (?:Mr\.|Mrs\.|Ms\.|Miss\.|Dr\.|Rev\.)
- (?:Mr\.|Mrs\.|Ms\.|Miss\.|Dr\.|Rev\.)
Example 4 - Mask Credit Card Numbers Except for the Last Four Digits
A tester wants to mask all but the last four digits of credit card numbers.
This example will use two linked generators:
- CreditCardGen - Generates Visa, Mastercard, and Discover card numbers.
- StringReplacementGen - Replaces all but the last four digits of the card number with an asterisk (*).
CreditCardGen
Discover, Mastercard, and Visa have been enabled for this example. The increment has been set to 1000 to ensure the last four digits have some variation.
StringReplacementGen
All but the last four digits will be replaced with an asterisk (*).
- value - references the CreditCardNumberGen (cardNumber).
- fromText- uses the following regular expression to identify the digits that need to be replaced
- .(?=.{4})
- toText - asterisk (*) has been entered as the replacement character