Use Regular Expression to capture information from user input

In Teneo Studio, you can easily use Regular Expression matcher to capture a certain pattern in the user input. For example, let’s say we need to recognize a Spanish phone number which usually contains 9 digits, begin with 6,7 or 9. You can follow the steps below to set up a Global Listener to capture all Spanish phone numbers mentioned in the user input:

  • Create a global variable named lPhoneNumbers with default value as empty list: []
  • Create a new global pre-listener named Get Spanish phone numbers
  • Add %TRUE.SCRIPT as condition, which means that this listener will be applied to every user input.
  • Add the following code as execution script:
import java.util.regex.Pattern
import java.util.regex.Matcher
String patternString = /(^|\s)([679]\d{8})($|\s|\p{Punct})/
Pattern pattern = Pattern.compile(patternString)
Matcher matcher = pattern.matcher(_.userInputText)
while(matcher.find()) {
            lPhoneNumbers <<        

Now test it in tryout. When you say, “My first number is 935988766 and my second number is 670548912.”, the two phone numbers in the input will be registered in the global variable iPhoneNumbers :
example variable

This post is an example on how you can capture certain patterns in user input via Regular Expressions. You can change the pattern string in the code according to your use case, for example the following pattern string for UK postcodes: /(^|\s)(([A-Z]{1,2}\d[A-Z\d]? ?\d[A-Z]{2}|GIR ?0A{2}))($|\s|\p{Punct})/. I hope this code example can help in your journey with Teneo Studio!

1 Like

Just a quick note for anyone coming here, to say that with the latest release (7.1) it is possible to add multiple global scripts of the same type, so the above use case could be covered as a new pre-matching script instead of an “always match” listener :partying_face::

def matches = _.userInputText =~ /(^|\s)([679]\d{8})($|\s|\p{Punct})/
iPhoneNumbers = matches.results().collect { }


1 Like