Skip to content

Instantly share code, notes, and snippets.

@xkef
Last active April 20, 2019 20:42
Show Gist options
  • Save xkef/395d51f336492523c02c08155830502b to your computer and use it in GitHub Desktop.
Save xkef/395d51f336492523c02c08155830502b to your computer and use it in GitHub Desktop.
regex.md
Display the source blob
Display the rendered blob
Raw
<svg xmlns="http://www.w3.org/2000/svg" xmlns:cc="http://creativecommons.org/ns#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" version="1.1" width="1909.562744140625" height="199">
<defs>
<style type="text/css">svg {
background-color: #fff; }
.root text,
.root tspan {
font: 12px Arial; }
.root path {
fill-opacity: 0;
stroke-width: 2px;
stroke: #000; }
.root circle {
fill: #6b6659;
stroke-width: 2px;
stroke: #000; }
.anchor text, .any-character text {
fill: #fff; }
.anchor rect, .any-character rect {
fill: #6b6659; }
.escape text, .charset-escape text, .literal text {
fill: #000; }
.escape rect, .charset-escape rect {
fill: #bada55; }
.literal rect {
fill: #dae9e5; }
.charset .charset-box {
fill: #cbcbba; }
.subexp .subexp-label tspan,
.charset .charset-label tspan,
.match-fragment .repeat-label tspan {
font-size: 10px; }
.repeat-label {
cursor: help; }
.subexp .subexp-label tspan,
.charset .charset-label tspan {
dominant-baseline: text-after-edge; }
.subexp .subexp-box {
stroke: #908c83;
stroke-dasharray: 6,2;
stroke-width: 2px;
fill-opacity: 0; }
.quote {
fill: #908c83; }
</style>
</defs>
<metadata>
<rdf:rdf>
<cc:license rdf:about="http://creativecommons.org/licenses/by/3.0/">
<cc:permits rdf:resource="http://creativecommons.org/ns#Reproduction"></cc:permits>
<cc:permits rdf:resource="http://creativecommons.org/ns#Distribution"></cc:permits>
<cc:requires rdf:resource="http://creativecommons.org/ns#Notice"></cc:requires>
<cc:requires rdf:resource="http://creativecommons.org/ns#Attribution"></cc:requires>
<cc:permits rdf:resource="http://creativecommons.org/ns#DerivativeWorks"></cc:permits>
</cc:license>
</rdf:rdf>
</metadata>
<desc>Created with Snap</desc><g class="root" transform="matrix(1,0,0,1,15,10.2969)"><g class="regexp match" transform="matrix(1,0,0,1,10,0)"><path d="M1645.0002,112.85944999999998H1685.0002M1767.859575,112.85945H1792.8596"></path><g class="match-fragment subexp" transform="matrix(1,0,0,1,0,0)"><rect rx="3" ry="3" class="subexp-box" transform="matrix(1,0,0,1,0,11.2969)" width="1665.000244140625" height="167.40625"></rect><text x="0" y="0" class="subexp-label" transform="matrix(1,0,0,1,0,11.2969)"><tspan>group #1</tspan></text><g class="regexp match" transform="matrix(1,0,0,1,10,21.2969)"><path d="M1068.359575,91.56254999999999H1138.3596"></path><g class="match-fragment subexp" transform="matrix(1,0,0,1,0,0)"><rect rx="3" ry="3" class="subexp-box" transform="matrix(1,0,0,1,0,11.2969)" width="1103.359619140625" height="135.8125"></rect><text x="0" y="0" class="subexp-label" transform="matrix(1,0,0,1,0,11.2969)"><tspan>group #2</tspan></text><g class="regexp match" transform="matrix(1,0,0,1,10,21.2969)"><path d="M331.00007500000004,70.26565H401.0001M694.343875,70.26565H764.3439"></path><g class="match-fragment subexp" transform="matrix(1,0,0,1,0,0)"><rect rx="3" ry="3" class="subexp-box" transform="matrix(1,0,0,1,0,11.2969)" width="356.0000915527344" height="104.21875"></rect><text x="0" y="0" class="subexp-label" transform="matrix(1,0,0,1,0,11.2969)"><tspan>group #3</tspan></text><g class="regexp match" transform="matrix(1,0,0,1,10,21.2969)"><path d="M208.14069577636718,48.96875H238.1407"></path><g class="match-fragment subexp" transform="matrix(1,0,0,1,0,0)"><rect rx="3" ry="3" class="subexp-box" transform="matrix(1,0,0,1,0,11.1875)" width="218.14068603515625" height="69.84375"></rect><text x="0" y="0" class="subexp-label" transform="matrix(1,0,0,1,0,11.1875)"><tspan>positive lookahead</tspan></text><g class="regexp match" transform="matrix(1,0,0,1,10,21.1875)"><path d="M97.859375,27.78125H122.8594"></path><g class="match-fragment" transform="matrix(1,0,0,1,0,6.125)"><path d="M0,21.65625q10,0 10,-10v-1.65625q0,-10 10,-10h72.859375q10,0 10,10v1.65625q0,10 10,10M10,6.65625l5,5m-5,-5l-5,5M15,21.65625q-10,0 -10,10v1.65625q0,10 10,10h82.859375q10,0 10,-10v-1.65625q0,-10 -10,-10"></path><g class="any-character" transform="matrix(1,0,0,1,15,10)"><g class="label"><rect width="82.859375" height="23.3125"></rect><text x="0" y="0" transform="matrix(1,0,0,1,5,16.6563)"><tspan>any character</tspan></text></g></g></g><g class="match-fragment charset" transform="matrix(1,0,0,1,122.8594,0)"><rect rx="3" ry="3" class="charset-box" transform="matrix(1,0,0,1,0,11.1875)" width="75.28129577636719" height="33.1875"></rect><text x="0" y="0" class="charset-label" transform="matrix(1,0,0,1,0,11.1875)"><tspan>One of:</tspan></text><g transform="matrix(1,0,0,1,5,16.1875)"><g class="charset-range" transform="matrix(1,0,0,1,0,0)"><text x="0" y="0" transform="matrix(1,0,0,1,30.9844,15.7969)">-</text><g class="literal" transform="matrix(1,0,0,1,0,0)"><g class="label"><rect width="25.984375" height="23.1875" rx="3" ry="3"></rect><text x="0" y="0" transform="matrix(1,0,0,1,5,16.5938)"><tspan class="quote">“</tspan><tspan>A</tspan><tspan class="quote">”</tspan></text></g></g><g class="literal" transform="matrix(1,0,0,1,39.9688,0)"><g class="label"><rect width="25.3125" height="23.1875" rx="3" ry="3"></rect><text x="0" y="0" transform="matrix(1,0,0,1,5,16.5938)"><tspan class="quote">“</tspan><tspan>Z</tspan><tspan class="quote">”</tspan></text></g></g></g></g></g></g></g><g class="match-fragment" transform="matrix(1,0,0,1,228.1407,37.3125)"><path d="M10,11.65625q-10,0 -10,10v1.65625q0,10 10,10h82.859375q10,0 10,-10v-1.65625q0,-10 -10,-10M102.859375,26.65625l5,-5m-5,5l-5,-5"></path><g class="any-character" transform="matrix(1,0,0,1,10,0)"><g class="label"><rect width="82.859375" height="23.3125"></rect><text x="0" y="0" transform="matrix(1,0,0,1,5,16.6563)"><tspan>any character</tspan></text></g></g><text x="0" y="0" class="repeat-label" transform="matrix(1,0,0,1,69.7969,44.5)"><tspan>1+ times</tspan><title><text x="0" y="0">repeats 2+ times in total</text></title></text></g></g></g><g class="match-fragment subexp" transform="matrix(1,0,0,1,366.0001,0)"><rect rx="3" ry="3" class="subexp-box" transform="matrix(1,0,0,1,0,11.2969)" width="353.34381103515625" height="104.21875"></rect><text x="0" y="0" class="subexp-label" transform="matrix(1,0,0,1,0,11.2969)"><tspan>group #4</tspan></text><g class="regexp match" transform="matrix(1,0,0,1,10,21.2969)"><path d="M205.4843771118164,48.96875H235.4844"></path><g class="match-fragment subexp" transform="matrix(1,0,0,1,0,0)"><rect rx="3" ry="3" class="subexp-box" transform="matrix(1,0,0,1,0,11.1875)" width="215.484375" height="69.84375"></rect><text x="0" y="0" class="subexp-label" transform="matrix(1,0,0,1,0,11.1875)"><tspan>positive lookahead</tspan></text><g class="regexp match" transform="matrix(1,0,0,1,10,21.1875)"><path d="M97.859375,27.78125H122.8594"></path><g class="match-fragment" transform="matrix(1,0,0,1,0,6.125)"><path d="M0,21.65625q10,0 10,-10v-1.65625q0,-10 10,-10h72.859375q10,0 10,10v1.65625q0,10 10,10M10,6.65625l5,5m-5,-5l-5,5M15,21.65625q-10,0 -10,10v1.65625q0,10 10,10h82.859375q10,0 10,-10v-1.65625q0,-10 -10,-10"></path><g class="any-character" transform="matrix(1,0,0,1,15,10)"><g class="label"><rect width="82.859375" height="23.3125"></rect><text x="0" y="0" transform="matrix(1,0,0,1,5,16.6563)"><tspan>any character</tspan></text></g></g></g><g class="match-fragment charset" transform="matrix(1,0,0,1,122.8594,0)"><rect rx="3" ry="3" class="charset-box" transform="matrix(1,0,0,1,0,11.1875)" width="72.62497329711914" height="33.1875"></rect><text x="0" y="0" class="charset-label" transform="matrix(1,0,0,1,0,11.1875)"><tspan>One of:</tspan></text><g transform="matrix(1,0,0,1,5,16.1875)"><g class="charset-range" transform="matrix(1,0,0,1,0,0)"><text x="0" y="0" transform="matrix(1,0,0,1,29.6563,15.7969)">-</text><g class="literal" transform="matrix(1,0,0,1,0,0)"><g class="label"><rect width="24.65625" height="23.1875" rx="3" ry="3"></rect><text x="0" y="0" transform="matrix(1,0,0,1,5,16.5938)"><tspan class="quote">“</tspan><tspan>a</tspan><tspan class="quote">”</tspan></text></g></g><g class="literal" transform="matrix(1,0,0,1,38.6406,0)"><g class="label"><rect width="23.984375" height="23.1875" rx="3" ry="3"></rect><text x="0" y="0" transform="matrix(1,0,0,1,5,16.5938)"><tspan class="quote">“</tspan><tspan>z</tspan><tspan class="quote">”</tspan></text></g></g></g></g></g></g></g><g class="match-fragment" transform="matrix(1,0,0,1,225.4844,37.3125)"><path d="M10,11.65625q-10,0 -10,10v1.65625q0,10 10,10h82.859375q10,0 10,-10v-1.65625q0,-10 -10,-10M102.859375,26.65625l5,-5m-5,5l-5,-5"></path><g class="any-character" transform="matrix(1,0,0,1,10,0)"><g class="label"><rect width="82.859375" height="23.3125"></rect><text x="0" y="0" transform="matrix(1,0,0,1,5,16.6563)"><tspan>any character</tspan></text></g></g><text x="0" y="0" class="repeat-label" transform="matrix(1,0,0,1,69.7969,44.5)"><tspan>1+ times</tspan><title><text x="0" y="0">repeats 2+ times in total</text></title></text></g></g></g><g class="match-fragment subexp" transform="matrix(1,0,0,1,729.3439,0)"><rect rx="3" ry="3" class="subexp-box" transform="matrix(1,0,0,1,0,11.2969)" width="354.01568603515625" height="104.21875"></rect><text x="0" y="0" class="subexp-label" transform="matrix(1,0,0,1,0,11.2969)"><tspan>group #5</tspan></text><g class="regexp match" transform="matrix(1,0,0,1,10,21.2969)"><path d="M206.1562521118164,48.96875H236.1563"></path><g class="match-fragment subexp" transform="matrix(1,0,0,1,0,0)"><rect rx="3" ry="3" class="subexp-box" transform="matrix(1,0,0,1,0,11.1875)" width="216.15625" height="69.84375"></rect><text x="0" y="0" class="subexp-label" transform="matrix(1,0,0,1,0,11.1875)"><tspan>positive lookahead</tspan></text><g class="regexp match" transform="matrix(1,0,0,1,10,21.1875)"><path d="M97.859375,27.78125H122.8594"></path><g class="match-fragment" transform="matrix(1,0,0,1,0,6.125)"><path d="M0,21.65625q10,0 10,-10v-1.65625q0,-10 10,-10h72.859375q10,0 10,10v1.65625q0,10 10,10M10,6.65625l5,5m-5,-5l-5,5M15,21.65625q-10,0 -10,10v1.65625q0,10 10,10h82.859375q10,0 10,-10v-1.65625q0,-10 -10,-10"></path><g class="any-character" transform="matrix(1,0,0,1,15,10)"><g class="label"><rect width="82.859375" height="23.3125"></rect><text x="0" y="0" transform="matrix(1,0,0,1,5,16.6563)"><tspan>any character</tspan></text></g></g></g><g class="match-fragment charset" transform="matrix(1,0,0,1,122.8594,0)"><rect rx="3" ry="3" class="charset-box" transform="matrix(1,0,0,1,0,11.1875)" width="73.29684829711914" height="33.1875"></rect><text x="0" y="0" class="charset-label" transform="matrix(1,0,0,1,0,11.1875)"><tspan>One of:</tspan></text><g transform="matrix(1,0,0,1,5,16.1875)"><g class="charset-range" transform="matrix(1,0,0,1,0,0)"><text x="0" y="0" transform="matrix(1,0,0,1,29.6563,15.7969)">-</text><g class="literal" transform="matrix(1,0,0,1,0,0)"><g class="label"><rect width="24.65625" height="23.1875" rx="3" ry="3"></rect><text x="0" y="0" transform="matrix(1,0,0,1,5,16.5938)"><tspan class="quote">“</tspan><tspan>0</tspan><tspan class="quote">”</tspan></text></g></g><g class="literal" transform="matrix(1,0,0,1,38.6406,0)"><g class="label"><rect width="24.65625" height="23.1875" rx="3" ry="3"></rect><text x="0" y="0" transform="matrix(1,0,0,1,5,16.5938)"><tspan class="quote">“</tspan><tspan>9</tspan><tspan class="quote">”</tspan></text></g></g></g></g></g></g></g><g class="match-fragment" transform="matrix(1,0,0,1,226.1563,37.3125)"><path d="M10,11.65625q-10,0 -10,10v1.65625q0,10 10,10h82.859375q10,0 10,-10v-1.65625q0,-10 -10,-10M102.859375,26.65625l5,-5m-5,5l-5,-5"></path><g class="any-character" transform="matrix(1,0,0,1,10,0)"><g class="label"><rect width="82.859375" height="23.3125"></rect><text x="0" y="0" transform="matrix(1,0,0,1,5,16.6563)"><tspan>any character</tspan></text></g></g><text x="0" y="0" class="repeat-label" transform="matrix(1,0,0,1,69.7969,44.5)"><tspan>1+ times</tspan><title><text x="0" y="0">repeats 2+ times in total</text></title></text></g></g></g></g></g><g class="match-fragment subexp" transform="matrix(1,0,0,1,1113.3596,37.3125)"><rect rx="3" ry="3" class="subexp-box" transform="matrix(1,0,0,1,0,11.2969)" width="531.6405944824219" height="74.90625"></rect><text x="0" y="0" class="subexp-label" transform="matrix(1,0,0,1,0,11.2969)"><tspan>negative lookahead</tspan></text><g class="regexp match" transform="matrix(1,0,0,1,10,21.2969)"><path d="M97.859375,32.95315H132.8594M215.718775,32.95315H250.7188M333.578175,32.95315H358.5781"></path><g class="match-fragment" transform="matrix(1,0,0,1,0,11.2969)"><path d="M0,21.65625q10,0 10,-10v-1.65625q0,-10 10,-10h72.859375q10,0 10,10v1.65625q0,10 10,10M15,21.65625q-10,0 -10,10v1.65625q0,10 10,10h82.859375q10,0 10,-10v-1.65625q0,-10 -10,-10M107.859375,36.65625l5,-5m-5,5l-5,-5"></path><g class="any-character" transform="matrix(1,0,0,1,15,10)"><g class="label"><rect width="82.859375" height="23.3125"></rect><text x="0" y="0" transform="matrix(1,0,0,1,5,16.6563)"><tspan>any character</tspan></text></g></g></g><g class="match-fragment subexp" transform="matrix(1,0,0,1,122.8594,0)"><rect rx="3" ry="3" class="subexp-box" transform="matrix(1,0,0,1,0,11.2969)" width="102.859375" height="43.3125"></rect><text x="0" y="0" class="subexp-label" transform="matrix(1,0,0,1,0,11.2969)"><tspan>group #6</tspan></text><g class="regexp match match-fragment any-character" transform="matrix(1,0,0,1,10,21.2969)"><g class="label"><rect width="82.859375" height="23.3125"></rect><text x="0" y="0" transform="matrix(1,0,0,1,5,16.6563)"><tspan>any character</tspan></text></g></g></g><g class="match-fragment" transform="matrix(1,0,0,1,235.7188,11.2969)"><path d="M0,21.65625q10,0 10,-10v-1.65625q0,-10 10,-10h72.859375q10,0 10,10v1.65625q0,10 10,10M15,21.65625q-10,0 -10,10v1.65625q0,10 10,10h82.859375q10,0 10,-10v-1.65625q0,-10 -10,-10M107.859375,36.65625l5,-5m-5,5l-5,-5"></path><g class="any-character" transform="matrix(1,0,0,1,15,10)"><g class="label"><rect width="82.859375" height="23.3125"></rect><text x="0" y="0" transform="matrix(1,0,0,1,5,16.6563)"><tspan>any character</tspan></text></g></g></g><g class="match-fragment escape" transform="matrix(1,0,0,1,358.5781,21.2969)"><g class="label"><rect width="153.0625" height="23.3125" rx="3" ry="3"></rect><text x="0" y="0" transform="matrix(1,0,0,1,5,16.6563)"><tspan>Back reference (group = 1)</tspan></text></g></g></g></g></g></g><g class="match-fragment" transform="matrix(1,0,0,1,1675.0002,101.2032)"><path d="M10,11.65625q-10,0 -10,10v1.65625q0,10 10,10h82.859375q10,0 10,-10v-1.65625q0,-10 -10,-10M102.859375,26.65625l5,-5m-5,5l-5,-5"></path><g class="any-character" transform="matrix(1,0,0,1,10,0)"><g class="label"><rect width="82.859375" height="23.3125"></rect><text x="0" y="0" transform="matrix(1,0,0,1,5,16.6563)"><tspan>any character</tspan></text></g></g><text x="0" y="0" class="repeat-label" transform="matrix(1,0,0,1,75.6406,44.5)"><tspan>7 times</tspan><title><text x="0" y="0">repeats 8 times in total</text></title></text></g><g class="match-fragment" transform="matrix(1,0,0,1,1792.8596,101.2657)"><g class="label anchor"><rect width="66.703125" height="23.1875"></rect><text x="0" y="0" transform="matrix(1,0,0,1,5,16.5938)"><tspan>End of line</tspan></text></g></g></g><path d="M65,112.85944999999998H0M1869.562725,112.85944999999998H1879.562744140625"></path><circle cx="0" cy="112.85944999999998" r="5"></circle><circle cx="1879.562744140625" cy="112.85944999999998" r="5"></circle></g></svg>

Regex tricks

https://regexper.com image.svg is from there

regex visualizer

exrex: generate words from regex

regexr: teaching

http://www.pyregex.com/



- negative lookahead: ^(?!.*bar).*$

e.g.: cut out "id": n in json

^(?!.*("id": ".*.")).*$

- passwords: 

             ^(?=.*?[A-Z]{2,})  # min. 2 upper case
              (?=.*?[a-z]{2,})  # min. 2 lower case
              (?=.*?[0-9]{2,})  # min. 2 numericals
              .{8,8}$           # length between 8,8 => 8
              
              ^(?=.*?[A-Z]{2,})(?=.*?[a-z]{2,})(?=.*?[0-9]{2,})(?!.*(.).*\1).{8,8}$ 
              
              inserting (?!.*(.).*\1) constraints word to be only of unique characters
              
            (
               (
               (
                (?=.*?[A-Z]
               ).{2,}
              )
              (
                (?=.*?[a-z])
                .{2,}
              )
              (
                (?=.*?[0-9])
                .{2,}
              )
                )(?!.*(.).*\1)
            )
            .{8,8}$ 
            
            => ((((?=.*?[A-Z]).{2,})((?=.*?[a-z]).{2,})((?=.*?[0-9]).{2,}))(?!.*(.).*\1)).{8,8}$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment