Perspective Key Event Regex New Line

Hi,

I’m trying to catch an RFID read using the Perspective key events to go straight to the idP login.

The RFID badges are an 8 - 10 character hex code followed by a line feed. While I can catch the code, as soon as I try include the line feed, nothing works.

This is the regex: [A-Fa-f0-9]{8,10}[\n?\r] and it works perfectly in regex testers, such as regexr.com.

Any ideas?

Regards,
Deon

Has anyone else successfully matched a string with a line feed?

That doesn't look like the correct pattern. If you're looking for an OS-agnostic linefeed, you would want:
[A-Fa-f0-9]{8,10}\r?\n - Windows uses CRLF, so \r\n, Unix uses \n alone. You explicitly don't want a character set [] around that, since the square brackets change the escaping logic such that your pattern is asking for a hex string followed by any one character \n, \r, or ? - not an optional newline.

Building on that, if you want to still only match the code but retain the condition where it is followed by a new line, you can leverage a lookahead assertion, e.g. [A-Fa-f0-9]{8,10}(?=\r?\n).

I’ll be honest, I only use regex in self defence.

It still does not seem to pick up the new line. I tried [A-Fa-f0-9]{8,10}\r?\n and [A-Fa-f0-9]{8,10}(?=\r?\n)

If I change it to key down event on Enter, it does recognise the enter key as seen below:
image

system.util.getLogger('RFID').info('RFID key: ' + str(event.key))

image

Is the enter key perhaps different to a new line and needs to be captured in another way?

For what it’s worth, I’m using the RFIdeas PCProx USB keystroking reader to output the card serial number.

I’m convinced there is something strange here.

If I change my regex pattern to [A-Fa-f0-9]{8,10}\s it should match on any whitespace, including line feeds, tabs etc.

It doesn’t seem related to the RFID reader; I can’t get it to work on a keyboard either.

If I type ‘123456789’ followed by a space, I get a match. I type ‘123456789’ followed by either enter or tab, I don’t get a match.

Simplifying it even further, I can’t get .\s to match a character followed by enter, only on space.

Any help matching the pattern with an enter at the back would be greatly appreciated

Ugh, you’re right, there is something strange here. It seems that the regex is matching against some string representation of the typed key, not the actual underlying character. A regex like this works when you press enter:
.+(Enter)
image

Your workaround did the trick. Is this a bug or just a strange browser effect?

I have decided to implement the regex as ([0-9a-fA-F]{8,10})(?=Enter|\r?\n), so that it will match the line feed if this behaviour changes in future.

Thanks for the help.

I’m of the opinion this is a bug (and filed a ticket to that effect) but there may be some underlying browser…strangeness, I’ll say, at work here that I’m not aware of.

1 Like

This is expected behavior, all. At the core of the regex match is the data that we receive from a JavaScript KeyboardEvent because this executes in the scope of the browser. We’re assembling a character sequence to match against from an input source (your barcode scanner) that creates KeyboardEvents. A “new line” character shows up as a key press for the “Enter” key.

https://keycode.info/ is a pretty decent way to test what KeyboardEvent data your sequence would produce. It tests a key at a time, but knowing what your sequence looks like, you could type it out on your keyboard to generate the KeyboardEvent and observe the event.key value in the tool.

Check out the docs site for more info.

Hi Deon,

I have a question regarding your scanning solution. Do you always select a input field before you scan the RFID tag, or are you able to trigger the Key event without?

Thanks.

It continuously matches and calls system.perspective.login on matching 8-10 hexadecimal characters followed by enter.

It would also match and call the login if a user were to type this, we deemed that unlikely enough. The convenience of just tapping twice is more than worth it.

I would have preferred an existing session to be logged out if another user taps, but that is not the behaviour as described in this thread:

Hi, I've tried a couple of places but where do I configure the keystroke? I have a similar deployment as the original post but with an actual barcode scanner. The scanner is set to scan then send and send an Enter keystroke.

My regex is as follow: [5|R][0|T]...........\d\d\d\d(?=Enter)

Then I'm binding the first array of data to a string tag that is on a PLC but when I scann it doesn't write anything to it.