How can I create a regex validation expression to allow this string? -
i have regex validation expression (thanks clasg , marvel308):
at beginning of each row contain -?\d's allow (but not require) word "\left(". , @ end of each row contain -?\d's allow (but not require) word "\right)" how can edit validation expression satisfy this?
here's regex handles cases:
(?:(?:\$\$?|\\[$[(]?)\s*|^)\\begin{(\w+)}\s*(?:-?\d+\s*(?:&\s*|\\\\\n))+\\end{\1}\s*(?:\$\$?|\\[]$)]?)?$ edit
here's solution allows matching opening/closing *tags:
^((?p<matrix>\\begin{(?<token>\w+)}\s*(?:-?\d+\s*(?:&\s*|\\\\\n))+\\end{\g<token>})|\$\s*(?p>matrix)\s*\$|\$\$\s*(?p>matrix)\s*\$\$|\\\[\s*(?p>matrix)\s*\\\]|\\\(\s*(?p>matrix)\s*\\\))$ it uses subroutine - matrix - keep bit shorter.
here's explanation:
the gut of part of recognizes matrix:
\begin{name} 1 & 2 & 3 \\ 1 & -2 & 4 \\ 1 & 2 & 5 \\ \end{name} this done part
\\begin{(\w+)}\s*(?:-?\d+\s*(?:&\s*|\\\\\n))+\\end{\1} which matches backslash followed literal text begin. matches name of matrix inside curly brackets, capturing group one. thereafter matches number of positive or negative numbers (-?\d+) followed either ampersand (&) or 2 backslashes , newline (\\\\\n). these rows may repeat number of times. matches ending backslash , name (captured group 1) inside curly brackets \\end{\1}.
the first version allows (not mandatory) preceded , followed lines consisting of $, $$, \( , \[. no check made enveloping lines match each other.
the second version starts testing naked matrix using gut regex explained above, , doing stores regex subroutine named "matrix" - (?p<matrix>. difference between previous gut regex name of regex stored in named capture group - token. text end of matrix done \\end{\g<token>}) checking literal string inside curly brackets matches part captured group named token.
the test of naked regex followed alternations having allowed surrounding lines correct combinations of characters , using subroutine matrix between them. e.g. alternations
\$\s*(?p>matrix)\s*\$ checks (one) dollar sign, optional white space, actual matrix (by calling subroutine) followed matching terminating line, i.e. single dollar sign.
Comments
Post a Comment