assistance-engine/docs/developer.avapframework.com/appendices_5.md

26 lines
1.9 KiB
Markdown

## getRegex()
The `getRegex()` command searches for matches in a source string using a regular expression (regex) pattern and stores the result in a target variable. This command is useful for extracting specific parts of a string that match a defined pattern, such as email addresses, phone numbers, or any other structure defined by a regex.
### Parameters
* SourceVariable Type: `variable` Description: The variable containing the source string in which to search for regex pattern matches. This string is the text on which the regex search will be applied.
* rePattern Type: `variable` Description: The variable containing the regular expression (regex) pattern that defines what to search for in the source string. This pattern should follow standard regex rules, allowing the specification of sequences of characters to identify in the source string.
* TargetVariable Type: `variable` Description: The variable where the search result will be stored. Depending on the context and the pattern used, the result could be the first match found, all matches, or even specific groups within the match.
### Usage Example
```javascript
// Direct call with values:
sourceText = "Email: user@example.com and phone: 123-456-7890"
pattern = r"\\b\\d{3}-\\d{3}-\\d{4}\\b"
getRegex(sourceText, pattern, phoneNumber)
// Call using variables:
sourceText = "Visit our website at https://www.example.com for more information."
regexPattern = r"https?://\\S+"
getRegex(sourceText, regexPattern, foundURL)
```
In the first example, a phone number in the format `123-456-7890` is searched in the `sourceText` string and the result is stored in the `phoneNumber` variable. In the second example, a URL is extracted from the `sourceText` string using a regex that identifies URL patterns, and the result is stored in the `foundURL` variable.