regexstringbinaryextract

Regexp to extract binary from a string


Is it possible to have string and extract the binary value from it and additionally only get the last binary digit from the value:

String = FCW2026P4F6

this would output in binary:

01000110 01000011 01010111 00110010 00110000 00110010 00110110 01010000 00110100 01000110 00110110

Now I would actually only need the last binary digit 0 from this. Any way this can be done by pure regexp?

Thanks.

Have not tried anything yet as I don't think this is doable.


Solution

  • It is possible, but only under certain conditions. If you've got the possibility to use PCRE2, you can use an expression like this:

    (.*[BDFHJLNPRTVXZ02468]$)|(.*[ACEGIKMOQSUWY13579]$)
    

    and replace with

    ${1:+0:1}
    

    Try it here: https://regex101.com/r/uYCB86/2

    The expression is split into two parts. The first are all the even numbered characters and the second is the odd ones. Add characters as needed. They are grouped with an or in between like this (ending with even character)|(ending with odd character).
    The replacement says "if group one matches, replace with 0, otherwise 1".

    Mandatory message: Don't use regex for this.