Live data from Hacker News

Regex that only matches itself

codegolf.stackexchange.com

21–30 of 66 posts

Re: Regex that only matches itself

#21

I think I might be missing something. Wouldn't something like "a" be a valid 1-byte regular expression that only matches instances of "a"?

No. "a" produces a match when run on "a" and "bad" and "aaaa" and many more things.

The regex ^a$ only matches "a", but it doesn't match itself.

Re: Regex that only matches itself

#22
post #11
post #3

I wish I was as smart as these people.

I'm inclined to say that some people 'just get' certain things like this or complex SQL. How they got to 'getting it', I don't know.

For starters, they've probably spent 100 to 1000x more time doing it than you or I have.

Perhaps their talent is actually just an attraction to studying it really hard. I, for instance, do not have the patience to study regex so much as to ever get the point where I could easily answer this question.

Re: Regex that only matches itself

#23
post #21

I think I might be missing something. Wouldn't something like "a" be a valid 1-byte regular expression that only matches instances of "a"?

No. "a" produces a match when run on "a" and "bad" and "aaaa" and many more things. The regex ^a$ only matches "a", but it doesn't match itself.

That isn't a match...

I can still see the neat aspect of the post. But that is not a match.

Re: Regex that only matches itself

#24
post #7

Reminds me of a quine - a non-empty computer program which takes no input and produces a copy of its own source code as its only output https://en.m.wikipedia.org/wiki/Quine_(computing)

In the esolang community, the name for this variation of Quine is a Narcissist or Narcissus program. You can see a bunch of examples in a number of different languages on the Rosetta Code website.

https://rosettacode.org/wiki/Narcissist

Re: Regex that only matches itself

#25
post #7

Reminds me of a quine - a non-empty computer program which takes no input and produces a copy of its own source code as its only output https://en.m.wikipedia.org/wiki/Quine_(computing)

It also reminded me about quines, I especially like the Quine Relay project which uses 100 different programming languages (each producing an input for the next one) to finally produce its own code https://github.com/mame/quine-relay

Re: Regex that only matches itself

#26
post #3

I wish I was as smart as these people.

These are smart people of course, but it's easy to get overly impressed by these results because all the scaffolding is missing. Behind such a result, there is a lot of tinkering, many leads going into dead ends. By the time you hit a solution, you have in your mind a theory of why it works and you can start cutting it down by removing anything non essential. You end up with a clean result that seems almost magical.

Re: Regex that only matches itself

#27
post #11

Earlier quoted context omitted.

I'm inclined to say that some people 'just get' certain things like this or complex SQL. How they got to 'getting it', I don't know.

And monads.

My suspicion is that no one wants to explain monads because if they did it would be painfully obvious that they're a purgatorial way of getting around the basic mismatch between functional purity and actual computers.

Re: Regex that only matches itself

#28
post #23
post #21

Earlier quoted context omitted.

No. "a" produces a match when run on "a" and "bad" and "aaaa" and many more things. The regex ^a$ only matches "a", but it doesn't match itself.

That isn't a match... I can still see the neat aspect of the post. But that is not a match.

What do you mean?

Re: Regex that only matches itself

#29
post #17

Earlier quoted context omitted.

> NOTE, delimiters must be included Your regex is actually /a/ which does not match the string "/a/".

I would say that adding slashes is quite arbitrary constraint, and it is really language specific syntax that is limited to minority of languages (I believe it was introduced in Perl). If you do regexp in Java, Python, C you don't use slashes. Edit: looks like slashes weren't even used in the example.

Okay, I'll quote some more from the question:

> […] for the sake of the challenge, let the expression be delimited (starting symbol, expression, ending symbol ex: /fancypantpattern/ or @[^2048]@), if you want to argue quotes as your delimiter, so be it. I think given the apparent difficulty of this problem it won't make much of a difference.

Re: Regex that only matches itself

#30

Earlier quoted context omitted.

And monads.

My suspicion is that no one wants to explain monads because if they did it would be painfully obvious that they're a purgatorial way of getting around the basic mismatch between functional purity and actual computers.

Actually, it's the other way around. Monad tutorials are so frequent that it's become a meme among Haskellers that everyone will eventually write a monad tutorial to relay his own mental model of monads.
Post reply on HN