Coffeescript regex doesn't match

I am trying to write a hubot script that responds to two different types of input. The user can either enter a stop name for local public transport, or perhaps a delayed postfix.

Thus, the input can be dvb zellescher weg

either dvb albertplatz

for the first option dvb zellescher weg in 5

or dvb albertplatz in 10

for the second. ("dvb" is the keyword here for my script and "zellescher weg" and "albertplatz" are two examples for stop names)

While trying to match them to regex, I ran into an issue where the regex I got to work on different testing sites (like regex101, which seems to be recommended here and does JS) won't work in my code, Regex to match the input without a number /^dvb (\D*)$/

, and I use /dvb\s+(.*)in (\d*)/

to match the cases where the user entered a delay.

The minimal example code for my hubot, which does not work for reasons unknown to me, looks like this:

robot.respond /^dvb (\D*)$/, (res) ->
    hst = res.match[1]
    res.send hst

      

Thanks for any help on this.

+3


source to share


1 answer


According to the source respond

comments on the code
:

# Public: adds a listener that tries to match incoming messages
# directed from a Regex based robot. All regular expressions treat patterns as they start
with #'^'

A regex from respond

jumps to respondPattern

, which avoids ^

and warns about the use of anchors:



if re[0] and re[0][0] is '^'
      @logger.warning \
        "Anchors don't work well with respond, perhaps you want to use 'hear'"

      

So, you either need to remove the shit ^

or use a method .hear

that doesn't use any "smart" regex. Preprocessing :

hear: (regex, options, callback) ->
    @listeners.push new TextListener(@, regex, options, callback)

      

+1


source







All Articles