Regex to *not* match any characters

前端 未结 10 1576
一个人的身影
一个人的身影 2020-12-24 11:18

I know it is quite some weird goal here but for a quick and dirty fix for one of our system we do need to not filter any input and let the corruption go into the system.

相关标签:
10条回答
  • 2020-12-24 11:44

    A simple and cheap regex that will never match anything is to match against something that is simply unmatchable, for example: \b\B.

    It's simply impossible for this regex to match, since it's a contradiction.

    References

    • regular-expressions.info\Word Boundaries
      • \B is the negated version of \b. \B matches at every position where \b does not.
    0 讨论(0)
  • 2020-12-24 11:45

    Have you tried this simple regex? [^.]*

    0 讨论(0)
  • 2020-12-24 11:46

    Another very well supported and fast pattern that would fail to match anything that is guaranteed to be constant time:

    $unmatchable pattern $anything goes here etc.

    $ of course indicates the end-of-line. No characters could possibly go after $ so no further state transitions could possibly be made. The additional advantage are that your pattern is intuitive, self-descriptive and readable as well!

    0 讨论(0)
  • 2020-12-24 11:52

    You want to match nothing at all? Neg lookarounds seems obvious, but can be slow, perhaps ^$ (matches empty string only) as an alternative?

    0 讨论(0)
提交回复
热议问题