Quinean Regex Tester

13

1

This challenge is pretty simple. As input, you take in a regular expression.

Then, you output a truthy/falsey of whether or not your source code matches the regular expression. It's that simple! Just two more things:

  • No quine builtins; you may, however, access the code's source code by file IO, etc.
  • This is , so shortest code in bytes wins!

Example

If your source code was say, abc, an input of a\wc would return true and an input of a\dc would return false.

Maltysen

Posted 2016-01-12T02:26:51.383

Reputation: 25 023

Example please? – Mama Fun Roll – 2016-01-12T02:29:00.800

Would accessing the file/textarea in which the code is contained in count as a builtin? – Conor O'Brien – 2016-01-12T02:30:02.787

1@CᴏɴᴏʀO'Bʀɪᴇɴ no, that's fine. – Maltysen – 2016-01-12T02:31:07.040

For your example, would regex a return true or false? – Mama Fun Roll – 2016-01-12T02:33:02.260

2>

  • I don't think just a BRE with simple character classes is specific enough. What BRE features have to be supported? 2. \d is not special in BRE; it matches the character d. 3. Choosing a specific regex flavor restricts your challenge to languages that support it, and few languages support BRE. Is that intentional?
  • < – Dennis – 2016-01-12T02:33:15.457

    @Dennis I'm not sure how regex's and flavours and stuff work, BRE was a name someone recommended in chat, and I kind of went with it. What do you recommend so answers don't have to worry about flavor specific features. – Maltysen – 2016-01-12T02:34:46.553

    2I'd recommend leaving it up to the answerer. If language x uses regex flavor y by default, let it use that flavor in this challenge. – Dennis – 2016-01-12T02:36:12.987

    @Maltysen "Use whatever RegEx is native to the language, or, BRE" – Conor O'Brien – 2016-01-12T02:36:13.890

    @Dennis makes sense. – Maltysen – 2016-01-12T02:36:54.157

    Must the regex match the entire source code, or can it match a substring of the source code? – feersum – 2016-01-12T02:45:41.573

    @feersum substring (unless, of course, it uses ^$) – Maltysen – 2016-01-12T02:46:41.003

    6@Maltysen Why don't you add a substring example to the question body? – feersum – 2016-01-12T03:10:24.157

    Answers

    14

    Z shell, 12 bytes

    grep "$@"<$0
    

    Zsh conditionals understand only exit codes, and the scripts exits with 0 or 1 accordingly.

    In addition, this prints a non-empty string (the source code) for a match and an empty one for a mismatch, which could be as truthy/falsy values in combination with test/[.

    The program reads its own file, but according to this comment by the OP, this is allowed.

    Dennis

    Posted 2016-01-12T02:26:51.383

    Reputation: 196 637

    3Aaand Dennis won. ¯\(ツ) – Conor O'Brien – 2016-01-12T02:43:41.750

    This doesn't work. It breaks on patterns with spaces in them. – feersum – 2016-01-12T02:51:26.297

    @feersum Whoops! Thanks for pointing that out. I have edited my answer. – Dennis – 2016-01-12T02:53:08.620

    2Now it breaks if it is written to a file with spaces in the name. Or a file called -v. Or... – Ben Millwood – 2016-01-12T15:08:50.380

    @BenMillwood I'd normally say don't save it with such a file name, but switching to zsh makes it bullet proof without incrementing the byte count. – Dennis – 2016-01-12T15:24:49.960

    I suppose you could also require the file to be called a, and then add +1 to the byte count (but $0 --> a saves that byte, so it remains at 12 bytes). Becomes more portable, but less elegant. – Martin Ender – 2016-01-12T15:31:19.370

    10

    JavaScript (ES6), 39

    (f=_=>!!`(f=${f})()`.match(prompt()))()
    

    n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳

    Posted 2016-01-12T02:26:51.383

    Reputation: 5 683

    Just about to post this, but you beat me to it. Great job! – Mama Fun Roll – 2016-01-12T03:29:13.077

    12The beginning of your code looks like me when trying to understand this : (f=_=) – Nico – 2016-01-12T09:35:54.037

    9

    Python 3, 119 bytes

    This just looks cooler, IMO (and it doesn't read the file).

    (lambda i:print(bool(__import__('re').search(input(),i))))("(lambda i:print(bool(__import__('re').search(input(),i))))")
    

    Python 3, 67 bytes

    print(bool(__import__('re').search(input(),open(__file__).read())))
    

    Added after reading this comment.

    Zach Gates

    Posted 2016-01-12T02:26:51.383

    Reputation: 6 152

    int is shorter than bool. – cat – 2016-03-10T22:42:54.363

    7

    Julia, 64 54 bytes

    r=readline;show(ismatch(Regex(r()),open(r,@__FILE__)))
    

    Julia regular expressions use PCRE. While reading the source code of the file is a standard loophole for quines, in this case it has been explicitly allowed. Takes input with no trailing newline.

    Alex A.

    Posted 2016-01-12T02:26:51.383

    Reputation: 23 761

    3

    Japt, 22 bytes

    "+Q ³sAJ fU"+Q ³sAJ fU
    

    Standard quine framework with a few bytes added to fit this challenge. Truthy = match(es), falsy = null. Try it online!

             // Implicit: U = input string, A = 10, J = -1, Q = quotation mark
    "..."+Q  // Take this string and concatenate a quotation mark.
    ³        // Repeat three times.
    sAJ      // Slice off the first 10 and last 1 chars.
    fU       // Match U to the result.
    

    ETHproductions

    Posted 2016-01-12T02:26:51.383

    Reputation: 47 880

    2

    Jolf, 18 15 bytes

    Supports the JS flavour of RegEx, I hope that's okay. Try it here!.

     h$code.value#i
    

    Commented:

      $code.value#      the document's element "code" (the program container)
    _h            i     and output if it has (matches) the input string (i.e. regex)
    

    Conor O'Brien

    Posted 2016-01-12T02:26:51.383

    Reputation: 36 228

    In which browser does this work? Both Chrome and Firefox complain that x.step is not a function. – Dennis – 2016-01-12T14:27:16.637

    @Dennis Huh. I must have broken the interpreter last night. What else is wrong? I currently am unable to debug, am at school. – Conor O'Brien – 2016-01-12T15:02:48.980

    Good. Now add a shortcut to the document's element "code" so we can make it shorter. – user48538 – 2016-01-12T15:26:34.863

    @CᴏɴᴏʀO'Bʀɪᴇɴ It also gives a reference error for math. – Dennis – 2016-01-12T15:26:58.303

    @Dennis Ah, that's why. I forgot to update the HTML, I added math.js. I will revise when I arrive home, if that's not too late. (In about 4 ish hours) – Conor O'Brien – 2016-01-12T15:51:15.557

    2

    Mathematica, 63 bytes

    StringMatchQ[ToString[#0, InputForm], RegularExpression[#1]] & 
    

    Note the trailing space. Uses the standard Mma quine mechanism, and tests if it matches the regex.

    LegionMammal978

    Posted 2016-01-12T02:26:51.383

    Reputation: 15 731

    2

    Perl, 21 bytes

    open 0;$_=<0>=~$_
    

    17 bytes plus 4 bytes for -pl0. Run like this:

    echo open | perl -pl0 quinean
    

    The source file must contain only the code above (no shebang, no trailing newline). Outputs 1 if the regex matches and the empty string if it doesn't (the empty string is falsey in Perl).


    Four bytes can be saved if the input is guaranteed not to end in a newline:

    open 0;say<0>=~<>
    

    Run like this:

    echo -n open | perl -M5.010 quinean
    

    say requires Perl 5.10+ and must be enabled with -M5.010. According to Meta, "the -M5.010, when needed, is free," giving a score of 17 bytes.

    How it works

    This is a simple variation on the standard "cheating" quine:

    open 0;print<0>
    

    This opens the file named in $0 and reads the contents with <0>.

    $_=<0>=~$_ reads one line from the source file, does a regex match against the contents of $_ (which were read by the -p flag), and assigns the result to $_. -p prints $_ automatically at the end.

    ThisSuitIsBlackNot

    Posted 2016-01-12T02:26:51.383

    Reputation: 1 050

    1

    , 14 chars / 26 bytes (non-competitive)

    ⟮‼(ⒸⅩ222+ᶈ0)đï
    

    Try it here (Firefox only).

    Using a version with bug fixes written after the challenge.

    Explanation

    ⟮‼(ⒸⅩ222+ᶈ0)đï // implicit: ï=input
    ⟮               // copy block: copy following code for later use
     (ⒸⅩ222+ᶈ0)   // take convert 10222 to char, add stuff inside copy block
    ‼           đï // check if input matches resulting string
                   // implicit output
    

    NOTE: Copy blocks are NOT quine operators. They are meant to be more versatile alternatives to variable declarations.

    Mama Fun Roll

    Posted 2016-01-12T02:26:51.383

    Reputation: 7 234

    1I think you can save a byte by changing to 10. – lirtosiast – 2016-01-12T05:59:42.563