Snippets

Kaz Nishimura scripts

Created by Kaz Nishimura last modified
#!/usr/bin/gawk -f

BEGIN {
    print "syntax: regexp"
}

/^\s*[^#]/ {
    # Canonicalizes to a rooted pattern.
    /\// || sub(/^/, "**/")

    gsub(/\./, "\\.")
    gsub(/\?/, "[^/]")
    gsub(/\*/, "[^/]*")
    gsub(/\[\^\/\]\*\[\^\/\]\*/, ".*") # recovery for '**'

    sub(/^/, "^")
    /\/$/ || sub(/$/, "$")

    sub(/^\^\.\*\/\[\^\/\]\*/, "") || sub(/^\^\.\*\//, "(^|/)")
}

{
    print
}
#!/usr/bin/perl -p

BEGIN {
    print "syntax: regexp\n"
}

if (m,^\s*[^#],) {
    # Canonicalizes to a rooted pattern.
    m,/, or s,^,**/,;

    s,[\\\.\+],\\$&,g;
    s,\?,[^/],g;
    s,\*,[^/]*,g;
    s,\[\^/]\*\[\^/]\*/,(.*/)?,g; # recovery for '**/'

    # Turns to a whole-line pattern.
    s,^,^,;
    m,/$, or s,$,\$,;

    # Simplifies to an unrooted pattern if possible.
    s,^\^\(\.\*/\)\?,(^|/),;
    s,^\(\^\|/\)\[\^/]\*,,;
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.