Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Looking at the example, my immediate reaction was that the main advantage would be the `anything_but` method, relieving me from the cumbersome construction of stuff like this:

    (?:[^t]|t(?:[^r]|r(?:[^u]|u(?:[^m]|m[^p]))))
What a time-saver it would be to write

    anything_but("trump")
Except, then you look at the source code and see this:

    public Builder anythingBut(final String pValue) {
        return this.add("(?:[^" + sanitize(pValue) + "]*)");
    }
Sad face :(


Might be a use case for The Greatest Regex Trick Ever: http://www.rexegg.com/regex-best-trick.html

(which boils down to "trump|(what you want)" and then check the capture group to see if you got what you want).


Except not all applications of regular expressions allow for that trick to be used. Case in point: grep (which of course has the -v option which together with pipes does the whole negative match thing much more neatly anyway).


Why would you be constructing stuff like that? It consumes the input string up until it differs. When is that useful in a regex?


How else would you write that you want to match all strings that don't contain string X? If you were matching at a specific position, you should use a negative lookahead (?!xyz), but I think in some cases you might need the mess above.


I can't imagine a case where the mess would be useful at all.

Negative lookahead is the only way I can imagine this being possibly useful.

I.E. "Give me a string that's not trump and has a vowel in it".

Given "trunk", that mess above would match all of "trun". Would good is matching a prefix going to do ever?


If you concede that the standard regex construct [^x] is a useful construct at all -- match any character except x -- why would should such a negative match be restricted to only a single character? Why not also allow for, say, match anything except the following _two_ characters in a row?


> Why not also allow for, say, match anything except the following _two_ characters in a row?

That part could be useful, but not when the pattern matches a seemingly random number of characters in the process.

[^x] is more predictable. It matches a single character, or not at all.


That part could be useful [...]

Well, then by application of your name, it should be useful for any number of characters. They're not random -- they spell out a word you would like not to match.

I'm not sure what you mean with "predictable". Are you referring to readability of the expression? I totally give you that... which kinda was my whole argument to begin with.


Any substring of a shorter length than the no-no word spells out a word which doesn't match it.

My opening statement was that I can't see a practical use for it. I still can't. Maybe one exists. But I don't think we're getting anywhere.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: