Regular expression greater than and less than

前端 未结 3 1618
遥遥无期
遥遥无期 2021-01-18 09:14

I would like to match any one of these characters: < or > or <= or >= or =.

This one doe

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-18 09:29

    I'm not sure why you are using slashes (might have something with coldfusion that I'm not aware of, add them back if need be)... Your regex currently matches only one character. Try:

    [<=>]{1,2}
    

    If you want one regex to match only >, <, >=, <= and =, there will be a bit more to that. The REMatch() function in coldfusion will return all the matched results in an array, so it's important to specify delimiters or boundaries in one way or another, because it's like a findall in python, or preg_match_all in PHP (or flagging the global match).

    The boundary I think is the simplest is the \b:

    \b(?:[<>]=?|=)\b
    

    Here's a demo with g activated.

    And without these boundaries, here's what happens.

    EDIT: Didn't realise something about the spaces. Could perhaps be fixed with this?

    \b\s*(?:[<>]=?|=)\s*\b
    

提交回复
热议问题