Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex - characters after delimiter, limited to a number

I am trying to put together some regex to get only the first 16 characters after the :

blahblahblah:fakeblahfakeblahfakeblahfakeblah

I came up with /[^:]*$ but that matches everything after the colon and if I try to trim from there its actually starting at the last character.

like image 968
A.Taylor Avatar asked Dec 18 '25 13:12

A.Taylor


1 Answers

Use

(?<=:)[^:]{16}(?=[^:]*$)

See proof

Explanation

--------------------------------------------------------------------------------
  (?<=                     look behind to see if there is:
--------------------------------------------------------------------------------
    :                        ':'
--------------------------------------------------------------------------------
  )                        end of look-behind
--------------------------------------------------------------------------------
  [^:]{16}                 any character except: ':' (16 times)
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    [^:]*                    any character except: ':' (0 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    $                        before an optional \n, and the end of
                             the string
--------------------------------------------------------------------------------
  )                        end of look-ahead
like image 117
Ryszard Czech Avatar answered Dec 21 '25 02:12

Ryszard Czech



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!