Preface: IANAL, but I have been reading about software copyright law issues for many years, and I claim to "know a bit" about the subject.
Material that you find on the internet is not in the public domain by default, The public domain has a specific meaning in (US) copyright law (ref Wikipedia) which is (in layman's terms):
In the sense of intellectual property, works in the public domain are those whose exclusive intellectual property rights have expired, have been forfeited, or are inapplicable. For example, the works of Shakespeare and Beethoven, and most of the early silent films, are all now in the public domain by either being created before copyrights existed or leaving the copyright term. Examples for works not covered by copyright which are therefore in the public domain, are the formulae of Newtonian physics, cooking recipes, and all software before 1974. Examples for works actively dedicated into public domain by their authors are reference implementations of cryptographic algorithms, NIH's ImageJ, and the CIA's World Factbook. The term is not normally applied to situations where the creator of a work retains residual rights, in which case use of the work is referred to as "under license" or "with permission".
For material that you find on the internet, the author or the publisher typically has (at least) residual rights. The material is NOT in the public domain in the sense of copyright law, unless one of the above applies.
In most cases where material is published on the internet, there is an explicit statement somewhere of the terms under which people may copy the material. The problem arises when there is no explicit statement of permission; e.g. no software license. In that case, the courts would probably recognize an implied right for people to view (legally published) material using normal means. However, that is about the limit of it1. All other rights are implicitly reserved if there is no explicit copyright statement.
This means that it is technically a copyright violation to copy someone's unlicensed code into their question.
Whether someone could successfully sue Stack Exchange because a user did that is a different matter. There are all sorts of legal arguments that could be made in mitigation, not least that the actual damage to the copyright holder caused by the copyright violation is de minimus. The DMCA "safe harbor" rules may also apply, provided that Stack Exchange promptly takes down offending material when notified.
However, it is still a bad idea for people to do this. And it is unnecessary. If the OP doesn't copy his code into his Question when prompted to, there is a straightforward remedy: vote to close the Question, and move on.
In short, you are correct.
1 - This does not extend to reverse engineering URLs, scraping websites without permission, using 3rd-party tools to download videos from Youtube, and so in. These are all most likely copyright violations. Or worse.
Concerning the rextester case, there are a couple of other issues with that site that make this question a legal minefield.
- The snippets that people want to copy are derivative works of 1) the regex author and 2) the author of the rextester code templates. Probably, both parties have residual rights.
- There is no (obvious) way to identify the regex author.
- There appear to be no explicit Terms and Conditions for the rextester site to clarify the copyright issues.
- Placing something into the "public domain" is actually legally complicated.
The combination of these make it extremely uncertain who has residual copyright over the snippets, whether the site owner can (legally) place the snippets into the public domain, and whether a simple statement on his website like the one in his Answer would be sufficient. I would advise the site owner to talk to a copyright law professional; e.g. a lawyer. Get this sorted out by a professional.