Remove leading and trailing digits from a long number?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Austenite2
Posts: 2
Joined: 17 Sep 2021, 22:45

Remove leading and trailing digits from a long number?

Post by Austenite2 » 17 Sep 2021, 23:03

Hello,

I'm using a handheld barcode scanner on a 2d barcode, which is working fine, but the 2d bardcode is encoded with more digits (56) than the human readable code printed next to it (22 digits). There are no other barcodes available to scan.

What I would like to do is use AutoHotKey to strip the leading and trailing digits, which are consistent, and leave only the human readable part, which changes. This would let me scan the barcode into the multiple pieces of software the it can be required in.

Two examples:
01993126509999989102010049212500060409928008210125083000
01993126509999989102010049284230060409938008210125083000

Separated for readability:
019931265099999891 0201004921250006040992 8008210125083000
019931265099999891 0201004928423006040993 8008210125083000

The parts I want to keep are in bold, and are 22 digits long. The prefix is 18 digits, and the suffix is 16 digits.

Things I've tried:
  • Hotstrings, as a test - recognise one of the 56 digit examples above and replace with desired text - can only be 40 digits long
  • Hotstrings, to remove leading and trailing digits - works great to remove the leading digits, I can't then make it trigger for the trailing digits
  • Contact the printer of the barcode, no help available there unfortunately!
  • Capture the whole sequence and then handle programatically - I don't know where to start, hence this post!
Any help greatly appreciated.
SandyClams
Posts: 63
Joined: 02 Jul 2020, 11:55

Re: Remove leading and trailing digits from a long number?

Post by SandyClams » 18 Sep 2021, 00:29

this should work, assuming you have the sequence in a variable allDigits

strippedDigits := RegExReplace(allDigits, "^\d{18}(\d+)\d{16}$", "$1")
teadrinker
Posts: 4316
Joined: 29 Mar 2015, 09:41
Contact:

Re: Remove leading and trailing digits from a long number?

Post by teadrinker » 18 Sep 2021, 01:08

Or like this:

Code: Select all

num := "01993126509999989102010049212500060409928008210125083000"
MsgBox, % RegExReplace(num, "^.{18}|.{16}$")
User avatar
boiler
Posts: 16841
Joined: 21 Dec 2014, 02:44

Re: Remove leading and trailing digits from a long number?

Post by boiler » 18 Sep 2021, 01:16

Or this, which should be faster, but probably doesn’t matter in this case:

Code: Select all

num := "01993126509999989102010049212500060409928008210125083000"
MsgBox, % SubStr(num, 19, 22)
User avatar
boiler
Posts: 16841
Joined: 21 Dec 2014, 02:44

Re: Remove leading and trailing digits from a long number?

Post by boiler » 18 Sep 2021, 01:26

Interesting use of “or” in RegExReplace for that purpose. Learned a new technique. :thumbup:
Austenite2
Posts: 2
Joined: 17 Sep 2021, 22:45

Re: Remove leading and trailing digits from a long number?

Post by Austenite2 » 21 Sep 2021, 04:06

Thank you all for your help, I now have a working script!
Post Reply

Return to “Ask for Help (v1)”