regex word boundary excluding the hyphen

后端 未结 2 1288
Happy的楠姐
Happy的楠姐 2020-12-28 15:03

i need a regex that matches an expression ending with a word boundary, but which does not consider the hyphen as a boundary. i.e. get all expressions matched by



        
相关标签:
2条回答
  • 2020-12-28 15:42

    You can use a lookahead for this, the shortest would be to use a negative lookahead:

    type ([a-z])(?![\w-])
    

    (?![\w-]) would mean "fail the match if the next character is in \w or is a -".

    Here is an option that uses a normal lookahead:

    type ([a-z])(?=[^\w-]|$)
    

    You can read (?=[^\w-]|$) as "only match if the next character is not in the character class [\w-], or this is the end of the string".

    See it working: http://www.rubular.com/r/NHYhv72znm

    0 讨论(0)
  • 2020-12-28 15:53

    I had a pretty similar problem except I didn't want to consider the '*' as a boundary character. Here's what I did:

    \b(?<!\*)([^\s\*]+)\b(?!*)
    

    Basically, if you're at a word boundary, look back one character and don't match if the previous character was an '*'. If you're in the middle, don't match on a space or asterisk. If you're at the end, make sure the end isn't an asterisk. In your case, I think you could use \w instead of \s. For me, this worked in these situations:

    *word
    wo*rd
    word*
    
    0 讨论(0)
提交回复
热议问题