How to only match a single instance of a character?

前端 未结 3 2054
你的背包
你的背包 2021-01-24 16:23

Not quite sure how to go about this, but basically what I want to do is match a character, say a for example. In this case all of the following would not contain ma

3条回答
  •  被撕碎了的回忆
    2021-01-24 16:53

    Basically what I want to do is match any single a that has any other non a character around it (except for the start and end of the string).

    ^[^\sa]*\Ka(?=[^\sa]*$)
    

    DEMO

    \K discards the previously matched characters and lookahead assertes whether a match is possibel or not. So the above matches only the letter a which satifies the conditions.

    OR

    a{2,}(*SKIP)(*F)|a
    

    DEMO

提交回复
热议问题