A better version of regex 101
just need to update 101 correctly
2 files changed, +13 -75
+2-68src/App.jsx
| @@ -1,4 +1,5 @@ | ||
| 1 | 1 | import { useState } from "react"; |
| 2 | +import parseBuilder from "./builder"; | |
| 2 | 3 | |
| 3 | 4 | const placeholder = `Enter your variables here like below. |
| 4 | 5 | group1Delim:\\n |
| @@ -8,74 +9,6 @@ group1Delim:\\n | ||
| 8 | 9 | \t\tvar3:\\w |
| 9 | 10 | \t\tvar4:\\d`; |
| 10 | 11 | |
| 11 | -const buildToRegex = (build) => { | |
| 12 | - const re = buildToRegexRecursion(build, ""); | |
| 13 | - console.log(re); | |
| 14 | - return re; | |
| 15 | -}; | |
| 16 | - | |
| 17 | -const buildToRegexRecursion = (build, re) => { | |
| 18 | - if (!build?.length) return re; | |
| 19 | - const val = build.shift(); | |
| 20 | - if (val.name) re += `(?P<${val.name}>${val.regex}`; | |
| 21 | - else re += `(?:${val.regex}`; | |
| 22 | - if (val.children) re += buildToRegexRecursion(val.children, ""); | |
| 23 | - re += `${val.optional})`; | |
| 24 | - | |
| 25 | - return buildToRegexRecursion(build, re, val); | |
| 26 | -}; | |
| 27 | - | |
| 28 | -const parseBuilder = (str) => { | |
| 29 | - const r = parseBuilderRegex(0); | |
| 30 | - const m = r.exec(str); | |
| 31 | - const vars = parseBuilderRecursion([], m, 0); | |
| 32 | - return buildToRegex(vars); | |
| 33 | -}; | |
| 34 | - | |
| 35 | -const parseBuilderRecursion = (vars, pm, lvl) => { | |
| 36 | - if (!pm) return; | |
| 37 | - vars.push(pm.groups); | |
| 38 | - pm.input = pm.input.split(pm[0])[1]; | |
| 39 | - if (!pm.input.trim()) { | |
| 40 | - return vars; | |
| 41 | - } | |
| 42 | - | |
| 43 | - // get next on this lvl | |
| 44 | - const r = parseBuilderRegex(lvl); | |
| 45 | - const m = r.exec(pm.input); | |
| 46 | - | |
| 47 | - // get between if there is a next on this lvl | |
| 48 | - if (m) { | |
| 49 | - const children = pm.input.split(m[0])[0]; | |
| 50 | - const childR = parseBuilderRegex(lvl + 1); | |
| 51 | - const childM = childR.exec(children); | |
| 52 | - if (childM) { | |
| 53 | - pm.groups.children = []; | |
| 54 | - parseBuilderRecursion(pm.groups.children, childM, lvl + 1); | |
| 55 | - } | |
| 56 | - return parseBuilderRecursion(vars, m, lvl); | |
| 57 | - } | |
| 58 | - // gap between if there is not a next on this lvl | |
| 59 | - else { | |
| 60 | - const childR = parseBuilderRegex(lvl + 1); | |
| 61 | - const childM = childR.exec(pm.input); | |
| 62 | - if (childM) { | |
| 63 | - pm.groups.children = []; | |
| 64 | - parseBuilderRecursion(pm.groups.children, childM, lvl + 1); | |
| 65 | - } | |
| 66 | - return vars; | |
| 67 | - } | |
| 68 | - | |
| 69 | - // return this level with recursion | |
| 70 | -}; | |
| 71 | - | |
| 72 | -const parseBuilderRegex = (numTabs) => { | |
| 73 | - return new RegExp( | |
| 74 | - `^(\\t{${numTabs}})(?<name>\\w*)(?<optional>\\??):(?<regex>.*)$`, | |
| 75 | - "gm" | |
| 76 | - ); | |
| 77 | -}; | |
| 78 | - | |
| 79 | 12 | function App() { |
| 80 | 13 | const [state, setState] = useState({ |
| 81 | 14 | valid: true, |
| @@ -118,6 +51,7 @@ function App() { | ||
| 118 | 51 | state.build = []; |
| 119 | 52 | setState({ ...state }); |
| 120 | 53 | const parseBuild = parseBuilder(e.target.value); |
| 54 | + console.log(parseBuild); | |
| 121 | 55 | if (!parseBuild) setState({ valid: false }); |
| 122 | 56 | else setState({ valid: true }); |
| 123 | 57 | //regexInput.focus(); |
+11-7src/builder.js
| @@ -1,11 +1,13 @@ | ||
| 1 | -export const parseBuilder = (str) => { | |
| 1 | +const parseBuilder = (str) => { | |
| 2 | 2 | let lines = str.split("\n"); |
| 3 | 3 | const ref = {}; |
| 4 | 4 | for (let line of lines) { |
| 5 | - const lineData = | |
| 6 | - /^(?<tabs>\s*)(?<name>\w*)(?<optional>\??):(?<regex>.*)/.exec( | |
| 7 | - line | |
| 8 | - ).groups; | |
| 5 | + if (!line.trim()) continue; | |
| 6 | + const r = /^(?<tabs>\s*)(?<name>\w*)(?<optional>\??):(?<regex>.*)/.exec( | |
| 7 | + line | |
| 8 | + ); | |
| 9 | + if (!r) return ""; | |
| 10 | + const lineData = r.groups; | |
| 9 | 11 | const tabs = lineData.tabs.length; |
| 10 | 12 | if (!ref[tabs]) ref[tabs] = [lineData]; |
| 11 | 13 | else ref[tabs].push(lineData); |
| @@ -16,10 +18,10 @@ export const parseBuilder = (str) => { | ||
| 16 | 18 | else parent.children.push(lineData); |
| 17 | 19 | } |
| 18 | 20 | } |
| 19 | - return ref[0]; | |
| 21 | + return buildToRegex(ref[0]); | |
| 20 | 22 | }; |
| 21 | 23 | |
| 22 | -export const buildToRegex = (build) => { | |
| 24 | +const buildToRegex = (build) => { | |
| 23 | 25 | const re = buildToRegexRecursion(build, ""); |
| 24 | 26 | return re; |
| 25 | 27 | }; |
| @@ -34,3 +36,5 @@ const buildToRegexRecursion = (build, re) => { | ||
| 34 | 36 | |
| 35 | 37 | return buildToRegexRecursion(build, re); |
| 36 | 38 | }; |
| 39 | + | |
| 40 | +export default parseBuilder; |