1

I'm running into an issue using regex patterns in botkit conversations that I can't quite work through, and though I'm sure someone else will have a quick answer, I'm utterly stumped.

I'm building a conversation that will store user information in a JSON file, but I need a small amount of validation on the entries before I store them. Specifically, the inputs must either be a full name (any number of words greater than one with a space between them) or a domain username in the format of domain\name with no spaces and the correct domain.

Using RegExr, I came up with the following regEx expressions which match in that user interface but which will not match when placed in the "pattern" attribute of the botkit conversation node:

  • \w+( +\w+)+ for the any number of words with a space between them.
  • domain+(\\+\w+) for the specified domain + a username

But when I use these in the botkit conversation, they're not matching -- so I'm not quite clear on what I'm doing wrong.

Here's the code snippet in which these are being used:

bot.startConversation(message, function (err, convo) {
    convo.say("I don't know who you are in TFS. Can you tell me?");
    convo.addQuestion("You can say \"no\" or tell me your TFS name or your domain name (eg: \"domain\\username)", [
        {
            pattern: bot.utterances.no,
            callback: function (response, convo) {
                convo.say("Okay, maybe another time then.");
                convo.next();
            }
        },
        {
                pattern: '\w+( +\w+)+',
                callback: function (response, convo) {
                convo.say("You said your TFS name is " + response.text);
                convo.next();
            }
        },
        {
            pattern: 'domain+(\\+\w+)+',
            callback: function (response, convo) {
                convo.say("You said your TFS name is " + response.text);
                convo.next();
            }
        },
        {
            default: true,
            callback: function (response, convo) {
                convo.say("I didn't understand that.");
                convo.repeat();
                convo.next();
            } 
        }   
    ], {}, 'default');
ctgilley
  • 25
  • 6

1 Answers1

2

You need to use double backslashes, and fix the backslash before the closing single quote in the first regex string literal:

pattern: '\\w+( +\\w+)+',
pattern: 'domain(\\\\\\w+)+',

The first pattern:

  • \\w+ - 1+ word chars
  • ( +\\w+)+ - 1 or more sequences of 1 or more spaces and then 1 or more word chars

Domain regex:

  • domain - a domain
  • (\\\\\\w+)+ - 1 or more occurrences of
    • \\\\ - 1 backslash
    • \\w+ - 1 or more word chars.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Just realized that botkit is matching the domain name when it has spaces, have gone through many iterations and haven't figured it out, was wondering if you had any pointers there. The regex I've pieced together and tested elsewhere is `^(domain\\\\(\\w+)\\S*$)` which works fine on RegExr (doesn't match "domain\foo bar" but does match domain\foo") but is still matching in botkit when there's a space included in the response (which I want to throw an error for). Any help would be appreciated. – ctgilley Oct 27 '17 at 22:15
  • 1
    @ctgilley The `^(domain\\\\(\\w+)\\S*$)` pattern cannot match a space because `^` matches the start of the string, `domain` matches a literal string, then a backslash is matched, then 1 or more word chars, and then 0+ non-whitespace chars up to the end of string (`$`). The issue seems to be not related to the regex, but how the string is processed before passing to the regex engine. – Wiktor Stribiżew Oct 27 '17 at 22:18
  • Thanks, I actually think it's the matching sequence that I have it in, I need to prevent the first match (word + " " + word + " " + word) from including the domain name and backslash, then I think the other one will work to exclude the match. Ugh, I hate regex. :) – ctgilley Oct 27 '17 at 22:27
  • 1
    I fixed the issue by changing the order of operations and ensuring that my word match regEx had a start-of-string character in it `^\\w+( +\\w+)+`. That seems to have put a nail in the coffin of this one. – ctgilley Oct 27 '17 at 22:34