-1

I have a string logs as follow:

rgb(255, 255, 255) 0px 0px 0px 16px inset

I like to get the dynamic value, in the case above, 16. How do I write regex for the rule to match the last px then take out the digits between px and the space?

Dylan Hsiao
  • 121
  • 7

1 Answers1

0

Since you say you just want the number before the last px, I won't assume that the text inset is always there. I would say \b\d+(?=px\b(?!.*\b\d+px\b)) is probably your safest bet.

blhsing
  • 70,627
  • 6
  • 41
  • 76
  • 2
    In case someone is interested I'll explain how this one works: `\b\d+` matches the begining of words that are digits, `(?=` is like an "and" operator as it checks if the following condition is also met, `px` checks if px is after the digit `(?!` is like a "not" operator as it checks if the following condition does not occur, and finally `\b\d+px\b` checks the same pattern as the beginning. In short it checks that the pattern "digit+px" exists and that after it, the same pattern does not occur again, so matches the last occurence. –  Jul 01 '18 at 02:34
  • 1
    In case you want to test it, here it is in regex101. BTW, a very good site for creating and test your regexp's https://regex101.com/r/V11BVd/1 –  Jul 01 '18 at 02:37
  • 1
    yeah I was using Regex101 to test. Thanks for all the detailed posted above! – Dylan Hsiao Jul 01 '18 at 04:39