Community Patterns

2

Detect RGB/RGBA colors in CSS

Created·2022-05-31 10:30
Flavor·PCRE2 (PHP)
JS function to convert RGB(A) to HEX color for single values: /** @param {string} color the RGB(A) color @return {string} the HEX color */ function rgba2hex(color) { const rgb = color.replace(/,\s+/g, ',').match(/^rgba?\((.\d]+)[, ]+([.\d]+)[, ]+([.\d]+)[, ]?([.\d]+)?\)$/i); let hex = (parseInt(rgb[1], 10) | 1 [.\d]+)[, ]+(?[.\d]+)[, ]+(?[.\d]+)(?:\s?[,\/]\s?(?[.\d]+%?))?\)/i', static function (array $matches) { $matches['r'] = ceil($matches['r']); $matches['g'] = ceil($matches['g']); $matches['b'] = ceil($matches['b']); if (isset($matches['a'])) { if (str_ends_with($matches['a'], '%')) { // 2.55 is 1% $matches['a'] = 2.55 * (float) substr($matches['a'], -1); } else { if ($matches['a' === '.') { $matches['a'] = '0' . $matches['a']; } $matches['a'] = 255 * (float) $matches['a']; } $matches['a'] = ceil($matches['a']); $hex = dechex(($matches['r'] << 24) | ($matches['g'] << 16) | ($matches['b'] << 8) | $matches['a']); return '#' . str_pad($hex, 8, '0', STR_PAD_LEFT); } $hex = dechex(($matches['r'] << 16) | ($matches['g'] << 8) | $matches['b']); return '#' . str_pad($hex, 6, '0', STR_PAD_LEFT); }, $css ); } Test: $css = <<<'CSS' color: rgb(34, 12, 64, 0.6); color: rgba(34, 12, 64, 0.6); color: rgba(34, 12, 64, .6); color: rgb(34 12 64 / 0.6); color: rgba(34 12 64 / 0.3); color: rgb(34.0 12 64 / 60%); color: rgba(34.6 12 64 / 30%); color: rgba(0, 255, 255); color: rgba(0, 255, 255); color: rgba(0, 255, 255, .5); color: rgba(0, 255, 255, 0.5); color: rgba(0 255 255 / 0.5); CSS; echo rgb2Hex($css); Result: color: #220c4010; color: #220c4010; color: #220c4010; color: #220c4010; color: #220c4008; color: #220c4000; color: #230c4000; color: #00ffff; color: #00ffff; color: #00ffff0d; color: #00ffff0d; color: #00ffff0d;
Submitted by WinterSilence
0

Facebook Page/Profile. No profile.php. No /groups/

Created·2015-12-13 16:23
Flavor·PCRE (Legacy)
Example: http://www.facebook.com/profile.php?id=123456789 http://www.facebook.com/someusername https://www.facebook.com/0x20.be http://www.facebook.com/plugins/likebox.php?href=http%3A%2F%2Fwww.facebook.com%2Fpages%2F2leep%2F109473355777935&amp;width=300&amp;connections=4&amp;stream=false&amp;header=false&amp;height=250 Matches patterns such as: https://www.facebook.com/my_page_id => my_page_id http://www.facebook.com/my_page_id => my_page_id http://www.facebook.com/#!/my_page_id => my_page_id http://www.facebook.com/pages/Paris-France/Vanity-Url/123456?v=app_555 => 123456 http://www.facebook.com/pages/Vanity-Url/45678 => 45678 http://www.facebook.com/#!/page_with_1_number => page_with_1_number http://www.facebook.com/bounce_page#!/pages/Vanity-Url/45678 => 45678 http://www.facebook.com/bounce_page#!/my_page_id?v=app_166292090072334 => my_page_id http://www.facebook.com/my.page.is.great => my.page.is.great https://www.facebook.com/Babies.Fan.Page https://www.facebook.com/pages/Babies.Fan.Page/121166161229757 https://www.facebook.com/pages/GHOST-Caf%C3%A9/627191887397533?fref=ts decoded: /like.php?href=http://www.facebook.com/#!/pages/Schenectady-NY/1045-The-Team-ESPN-Radio/100568296308&amp;width&amp;layout=button&amp;action=like&amp;show_faces=false& amp;share=false&amp;height=35" scrolling="no" frameborder="0" style="overflow:hidden; width:50px; height:20px;" allowTransparency="true">--> decoded: /like.php?href=http://drupal7.arisiahosting.org&amp;send=false&amp;layout=standard&amp;width=230&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=l ucida+grande&amp;height=35" style="border:none; overflow:hidden; width:230px; height:35px;"> 14ers.com on Facebook
Submitted by aramosf
1

JavaScript number/bigint literals

Created·2024-02-14 23:07
Updated·2025-06-10 00:23
Flavor·ECMAScript (JavaScript)
Should match any valid JavaScript number/bigint literal. See this StackOverflow answer by me for matching JS numbers. open in regex101 editor - example matches and unit tests\ Regulex%7C%5BoO%5D%5B0-7%5D(%3F%3A_%3F%5B0-7%5D%2B)%7C%5B0-7%5D%2B%7C%5BxX%5D%5B0-9a-fA-F%5D(%3F%3A_%3F%5B0-9a-fA-F%5D%2B))%7C(%3F%3A(%3F%3A0%7C0%5B0-9%5D%5B89%5D%5B0-9%5D%7C%5B1-9%5D(%3F%3A_%3F%5B0-9%5D%2B))(%3F%3A%5C.(%3F%3A%5B0-9%5D(%3F%3A_%3F%5B0-9%5D%2B))%3F)%3F%7C%5C.%5B0-9%5D(%3F%3A_%3F%5B0-9%5D%2B))(%3F%3A%5BeE%5D%5B%2B-%5D%3F%5B0-9%5D(%3F%3A_%3F%5B0-9%5D%2B))%3F)%7C-%3F(%3F%3A0(%3F%3A%5BbB%5D%5B01%5D(%3F%3A_%3F%5B01%5D%2B)%7C%5BoO%5D%5B0-7%5D(%3F%3A_%3F%5B0-7%5D%2B)%7C%5BxX%5D%5B0-9a-fA-F%5D(%3F%3A_%3F%5B0-9a-fA-F%5D%2B))%3F%7C%5B1-9%5D(%3F%3A_%3F%5B0-9%5D%2B)*)n)%24 "View the RegExp on jex.im/regulex") - colored graph\ Regexper - slightly smaller and less colored graph +/- Infinity and NaN _ as numerical separator between any two digits (except with leading 0s) Number leading/trailing . (decimal point) leading 0s [!] interpreted as octal when all digits are less than 8 (then . or scientific notation will error) [!] doesn't allow numerical separator (in the integer part; even with digits above 7) prefix 0b binary no . or scientific notation 0o octal no . or scientific notation 0x hexadecimal no . or scientific notation BigInt explicit via suffix n no leading 0s except after a 0o/0x/0b prefix no +, ., or scientific notation Note, Number() (parsing from a string) allows leading/trailing whitespace and scientific notation, but not `, and leading 0`s are not an implicit octal prefix, (and ofc no _BigInt suffix)
Submitted by MAZ01001
0

974618121112

Created·2021-11-09 19:37
Flavor·Python
``Hash: 0xcba5ee76d1b322114c4e5b1a3e00edf1065bf0346bef88ee603a8a061908db88- https://img.shields.io/azure-devops/coverage/0xb95343413e459a0f97461812111254163ae53467855c0d73e0f1e7c5b8442fa3/https://tokenview.com/en/search/0xdf6ac64b05e8bb7ca5c8d31ff44a00544eb039a7f467a424ae1c1e1926f19684/974618121112?color=https%3A%2F%2Fimg.shields.io%2Fazure-devops%2Fcoverage%2F0xb95343413e459a0f97461812111254163ae53467855c0d73e0f1e7c5b8442fa3%2Fhttps%3A%2F%2Ftokenview.com%2Fen%2Fsearch%2F0xdf6ac64b05e8bb7ca5c8d31ff44a00544eb039a7f467a424ae1c1e1926f19684%2F974618121112&label=%3Cimg%20alt%3D%22Azure%20DevOps%20coverage%22%20src%3D%22https%3A%2F%2Fimg.shields.io%2Fazure-devops%2Fcoverage%2F0xb95343413e459a0f97461812111254163ae53467855c0d73e0f1e7c5b8442fa3%2Fhttps%3A%2F%2Ftokenview.com%2Fen%2Fsearch%2F0xdf6ac64b05e8bb7ca5c8d31ff44a00544eb039a7f467a424ae1c1e1926f19684%2F974618121112%22%3E&logo=0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2&logoColor=https%3A%2F%2Ftokenview.com%2Fen%2Fsearch%2F0xdf6ac64b05e8bb7ca5c8d31ff44a00544eb039a7f467a424ae1c1e1926f19684&style=plasticgmsUi image:: https://img.shields.io/azure-devops/coverage/0xb95343413e459a0f97461812111254163ae53467855c0d73e0f1e7c5b8442fa3/https://tokenview.com/en/search/0xdf6ac64b05e8bb7ca5c8d31ff44a00544eb039a7f467a424ae1c1e1926f19684/974618121112?color=https%3A%2F%2Fimg.shields.io%2Fazure-devops%2Fcoverage%2F0xb95343413e459a0f97461812111254163ae53467855c0d73e0f1e7c5b8442fa3%2Fhttps%3A%2F%2Ftokenview.com%2Fen%2Fsearch%2F0xdf6ac64b05e8bb7ca5c8d31ff44a00544eb039a7f467a424ae1c1e1926f19684%2F974618121112&label=%3Cimg%20alt%3D%22Azure%20DevOps%20coverage%22%20src%3D%22https%3A%2F%2Fimg.shields.io%2Fazure-devops%2Fcoverage%2F0xb95343413e459a0f97461812111254163ae53467855c0d73e0f1e7c5b8442fa3%2Fhttps%3A%2F%2Ftokenview.com%2Fen%2Fsearch%2F0xdf6ac64b05e8bb7ca5c8d31ff44a00544eb039a7f467a424ae1c1e1926f19684%2F974618121112%22%3E&logo=0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2&logoColor=https%3A%2F%2Ftokenview.com%2Fen%2Fsearch%2F0xdf6ac64b05e8bb7ca5c8d31ff44a00544eb039a7f467a424ae1c1e1926f19684&style=plastic :alt: Azure DevOps coverage1. -
Submitted by anonymous

Community Library Entry

0

Regular Expression
Created·2021-12-03 21:24
Flavor·Python

r"
(?P<Hex>(?<=.[^}{])#(?:[\dA-F]{6}|[\dA-F]{3})(?=\W[^}{]))+
"
ig
Open regex in editor

Description

A solution of this test from HackerRank

https://www.hackerrank.com/challenges/hex-color-code/problem

Submitted by anonymous