duanbi1888
2012-12-06 15:11
浏览 90
已采纳

正则表达式搜索2个字符串之间的可选通配符

I have seen similar problems but I cant quite get them to work for my scenario.

I am trying to use regex to find an optional wildcard between 2 strings. Here is an example of the strings that the regex does find correctly: not happy

if the string was: not very happy

The code must still match both cases.

The code I have is:

/\b(?<=not(*?).)happy\b/

(happy and very would be variables)

Any help pointing me in the right direction would be very much appreciated.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duai4512 2012-12-06 15:29
    已采纳

    Some tweaking may be required, but this would match up to two words in between "not" and "happy":

    /not(?:\s+\w+){0,2}\s+happy/
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doutan6286 2012-12-06 15:40

    This ...

    /not +([^ ]* +)?happy/
    

    And feel free to replace ? with {0,2} (for example) to match up to 2 words between not and happy.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题