Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Regex 201

Regex 201

A few quick tips on Regex for experienced peeps, given as a Tighten Talk on Dec 21, 2018

Jake Bathman

December 21, 2018
Tweet

More Decks by Jake Bathman

Other Decks in Programming

Transcript

  1. ANCHORS ^ $ \b Start of string End of string

    Word boundary ^([\w]+)\b([\w]+)$ Jake Bathman
  2. MATCH GROUPS • In PHP, Javascript, and other languages, you

    can name your match groups in regex matches /Call me (?<firstname>[\w]+)/ Call me Jake PHP & JS 
 syntax: PHP example: preg_match( '/Call me (?<firstname>[\w]+)/', "Call me Jake", $matches ); echo($matches['firstname']);
  3. BONUS: MY MOST INSANE REGEX (^([A-Za-z0-9]+)\:?)|(inet|inet6)\s*(?:addr:?)?\s*((\d{1,3}\.){3}\d{1,3}|((([0-9A-Fa-f]{1,4}:){7}([0-9A- Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9]) (\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}) {1,2})|:((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])) {3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4][0-9]|

    1[0-9][0-9]|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}))|:))|(([0-9A-Fa-f]{1,4}:) {3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9]) (\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}) {1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]| 1[0-9][0-9]|[1-9]?[0-9])){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f] {1,4}){0,4}:((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]? [0-9])){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4][0-9]|1[0-9] [0-9]|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}))|:)))(%.+)?) eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 45.56.76.94 netmask 255.255.255.0 broadcast 45.56.76.255 inet6 fe80::f03c:91ff:fea1:e9d1 prefixlen 64 scopeid 0x20<link> inet6 2600:3c00::f03c:91ff:fea1:e9d1 prefixlen 64 scopeid 0x0<global> ether f2:3c:91:a1:e9:d1 txqueuelen 1000 (Ethernet) RX packets 145749659 bytes 171250537930 (159.4 GiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 60215383 bytes 12025642918 (11.1 GiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eth0: inet 45.56.76.94 inet6 fe80::f03c:91ff:fea1:e9d1 inet6 2600:3c00::f03c:91ff:fea1:e9d1 ifconfig myip