Regex excluding specific characters

后端 未结 4 2194
你的背包
你的背包 2020-12-09 16:01

In this lesson I don\'t understand why [^b] is not correct? I understand that [^bog] is correct.

[^b] should match any string

4条回答
  •  隐瞒了意图╮
    2020-12-09 16:48

    You are fundamentally correct, but [^b] will still match o and g in bog -- meaning it is a successful match, even though it didn't match the whole string. [^bog] will only match h in hog, d in dog, and nothing in bog -- meaning it doesn't match bog.

    I think this will make more sense if you look at ^[^b]+$. This will match 1+ non-b characters, anchored at the beginning (^) and end ($) of the string. Comparing that to your initial expression of [^b] or [^bog], you can see the difference. I suggest using a GUI RegEx tester (the previously linked one is my favorite), which will really help illustrate the logic.

提交回复
热议问题