Regular expression greater than and less than

前端 未结 3 1612
遥遥无期
遥遥无期 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
    
    0 讨论(0)
  • 2021-01-18 09:38

    You don't need to escape any of those characters in character class. Apart from that, you need to use quantifiers, to match more than 1 repetition of those characters.

    You need this:

    [<>=]{1,2}
    

    Note the quantifier, to match 2 repetitions, as required for <= and >=.


    Also, note that this will also match - ==, <<. If you strictly want to match just those 4 strings, you can use this regex:

    [<>]=?|=
    

    Using ? after = makes it optional. So, first part will match - <, >, <=, and >=. And then we add = using pipe.

    0 讨论(0)
  • 2021-01-18 09:52

    Try this:

    [<>]=?|=
    

    It matches < or > optionally followed by =, or just = by itself.

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