Domanda

i want a regex that would check only contains of logical expressions: 0-9 == || != && etc...

this is my try so far:

/^([ \-+><0-9])|(&&)|(==)|(\x7C\x7C)|(!=)|(<=)|(>=)$/i

Im not sure how it works

http://regex101.com/r/xE4fY3

È stato utile?

Soluzione

You're only matching one expression for the whole line, try this:

/^(([ \-+><0-9])|(&&)|(==)|(\x7C\x7C)|(!=)|(<=)|(>=))+$/i

Altri suggerimenti

I created this little fella.

^\h*(\-?)([0-9]+)\h*(&&|==|\x7C\x7C|!=|<=|>=)\h*([0-9]*)$

The \h* is optional whitespace (spaces, tabs).

Then, the next/first part should be numbes (right?).

At least one Number. Optional negative or positive.

Followed by more optional whitespace.

Followed by the optional logical operators.

Followed by a number.

Not so different from Andresch Serj solution, a verbose version ( readable and higthly editable ):

$pattern = '~
    # subpattern definitions
    (?(DEFINE)
        (?<operator> && | [!<>=]?= | \x7C\x7C | [+-<>] )
        (?<operand> -?[0-9]+ )
    )

    # main pattern
    ^ \h* \g<operand> \h* (?: \g<operator> \h* \g<operand> )* \h* $
    ~x';
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top