I never fully understood existing regex
syntax and rules, so I chose to implements mine!
It is possible to generate regex rules for string or programmatically.
Some syntax:
a:z
(Range) returnTRUE
if the char is betweena
andz
included*
(All) returnTRUE
for any charabc
(Sequence) returnTRUE
if the sequenceabc
exist in the string at the iterator positionR1|R2
(Or) returnTRUE
if one of the 2 rules isTRUE
R1&R2
(And) returnTRUE
if both of the 2 rules areTRUE
#R1
(Any) returnTRUE
always and iterate until the rule isTRUE
=NR1
(Exact) returnTRUE
if the rule isTRUE
exactlyN
times>NR1
(Minimum) returnTRUE
if the rule isTRUE
at leastN
times<NR1
(Maximum) returnTRUE
if the rule isTRUE
maximumN
times(..)
(Nested Rule) returnTRUE
if the rule inside the parenthesis isTRUE
\
(Special chars) special chars such as|
or&
or\
or any other can be used in rules by adding\
before.
A simple regex to find markdown links is [(#!])]\((>1!\))\)
. To compile the rule:
Regex * r = parse("[(#!])]\\((>1!\\)))");
If you want to be sure that is not an image just put (!\!)
before the previous rule. Note that (!\!)
is equal to (*&!\!)
.
A more complex regex to find markdown tables is \|(>1!\|)\|(#!\n)\n\|(\:|-)(>1-&!((\:|-)\|)\|(#!\n)\n
the code is the following:
const char *str = "| A | B |\n"
"|:-:|--:|\n";
size_t n = strlen(str);
Regex * r = parse("\\|(>1!\\|)\\|(#!\n)\n"
"\\|(\\:|-)(>1-&!((\\:|-)\\|))(\\:|-)\\|(#!\n)\n");
if (!r) {
fprintf(stderr, "Parse error!");
return -1;
}
size_t i = 0;
Bool res = eval(str, &i, n, r);
printf("Res: %s, at %c (%lu)\n", (res ? "true":"false"), str[(i-1)], i);
regex_free(r);
The result of such code is
Res: true, at
(20)