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

Everybody Stand Back! I Know Regular Expressions

Everybody Stand Back! I Know Regular Expressions

More Decks by Андрей Листочкин (Andrey Listochkin)

Other Decks in Programming

Transcript

  1. SDS 940 1966 - QED 1968 - Mother of All

    Demos 1969 - 1st ARPANET IMP node
  2. s = s[4] === 's' ? s.substr(8) : s.substr(7); const

    iQ = s.indexOf('?'); const iH = s.indexOf('#'); const iS = s.indexOf('/'); if (Math.min(iQ, iH, iS) > 0) { return s.substr(0, Math.min(iQ, iH, iS)); } else { return s; }
  3. DSL

  4. function* enumerate(iterable) { let i = 0; for (const x

    of iterable) { yield [i, x]; i++; } }
  5. function matchAbc(string) { for (const [startPos] of enumerate(string)) { let

    matchPos = startPos; if (str[matchPos] !== 'a') continue; matchPos += 1; if (str[matchPos] !== 'b') continue; matchPos += 1; if (str[matchPos] !== 'c') continue; return true; } return false; }
  6. function matchAbc(string) { for (const [startPos] of enumerate(string)) { let

    matchPos = startPos; if (str[matchPos] !== 'a') continue; matchPos += 1; if (str[matchPos] !== 'b') continue; matchPos += 1; if (str[matchPos] !== 'c') continue; return true; } return false; }
  7. DSL

  8. function r(re, flags = 'u') { return new RegExp( re

    .replace(/^(?<regex>.*)(?<!\\)#.*$/gm, '$<regex>') .replace(/\\#/gm, '#') .replace(/\s/gm, ''), flags ); }
  9. function r(re, flags = 'u') { return new RegExp( re

    .replace(/^(?<regex>.*)(?<!\\)#.*$/gm, '$<regex>') .replace(/\\#/gm, '#') .replace(/\s/gm, ''), flags ); }
  10. function r(re, flags = 'u') { return new RegExp( re

    .replace(/^(.*)(?<!\\)#.*$/gm, '$1') .replace(/\\#/gm, '#') .replace(/\s/gm, ''), flags ); }
  11. /(?x) (?(DEFINE) (?<object> \{ (?&nvp) [, (?&nvp)]* \}) (?<nvp> (?&name)

    : (?&value) ) (?<value> (?&number) | (?&boolean) | (?&string) | (?&object) | (?&array) ) ... ) (?&value) /
  12. / :my regex sign { <[+-]> } :my regex mantissa

    { \d+: '.'?: \d*: | '.' \d+: } :my regex exponent { <[eE]> <sign>?: \d+: } <sign>? <mantissa> <exponent>? /
  13. function r(re, flags = 'u') { return new RegExp( re

    .replace(/^(?<regex>.*)(?<!\\)#.*$/gm, '$<regex>') .replace(/\\#/gm, '#') .replace(/\s/gm, ''), flags ); }
  14. [2020-09-11T15:57:20.848Z - 099fe80d-7e11-4c47-9bbf-2372bd6b2527: 108ms] 200 OK GET /configuration-tree/8 <- http://web.app.ui/

    178.133.180.67 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:82.0) Gecko/20100101 Firefox/82.0' {"id":1,"email":"[email protected]",...}
  15. [Time in UTC - Request ID: Response Time] [2020-09-11T15:57:20.848Z -

    099fe80d-7e11-4c47-9bbf-2372bd6b2527: 108ms] Status Code and Message Request Method and Path <- Refer URL User IP 200 OK GET /configuration-tree/8 <- http://web.app.ui/ 178.133.180.67 User Agent 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:82.0) Gecko/20100101 Firefox/82.0' Session Token {"id":1,"email":"[email protected]",...}
  16. ^ \[ (?<requestDate>2020-0[789]\S+) \s - \s (\S+) # request ID

    : \s (?<requestDuration>\d+)ms ] \s .+ \s (?<session>\S+) (?<!null) # non-anonymous sessions only $
  17. const match = lineRe.exec(logLine); if (match) { const { requestDate,

    requestDuration, session } = match; const requestData = { date: new Date(requestDate), duration: parseInt(requestDuration, 10), email: JSON.parse(session).email } }