40

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

[^b] should match any string that has no b character and don't match any string containing any b character.

Is there anything wrong in my understanding?

baduker
  • 19,152
  • 9
  • 33
  • 56
SaidbakR
  • 13,303
  • 20
  • 101
  • 195
  • 5
    `[^b]` matches a character that is not a `b`. So in your `hog / bog / dog` example, it matches all of them since all words have a letter in them that is not a `b`. `[^bog]` actually matches any character that is neither a `b`, nor `o`, nor `g`, that's why it does not match any letter of `bog`. Neither would it match `gob` or `ogb` etc. The answer to that question, by the way, could just be `[dh]og` to match `dog`, `hog`, but not `bog`. – Daniël Knippers Apr 28 '14 at 22:17

5 Answers5

45

For that specific lesson, the correct regex is:

[^b]og

EXPLANATION:

/[^b]og/

[^b] match a single character not present in the list below
b the literal character b (case sensitive)
og matches the characters og literally (case sensitive)

NOTES:

Negated Character Classes

Typing a caret after the opening square bracket negates the character class. The result is that the character class matches any character that is not in the character class. Unlike the dot, negated character classes also match (invisible) line break characters. If you don't want a negated character class to match line breaks, you need to include the line break characters in the class. [^0-9\r\n] matches any character that is not a digit or a line break.

It is important to remember that a negated character class still must match a character. q[^u] does not mean: "a q not followed by a u". It means: "a q followed by a character that is not a u". It does not match the q in the string Iraq. It does match the q and the space after the q in Iraq is a country. Indeed: the space becomes part of the overall match, because it is the "character that is not a u" that is matched by the negated character class in the above regexp. If you want the regex to match the q, and only the q, in both strings, you need to use negative lookahead.

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • I +1'ed yours because some reason I didn't think of the expression they were actually looking for. However, I think you could answer OP's direct question better. – Sam Apr 28 '14 at 22:33
  • @sam I think the ***`explanation`*** is self explanatory. – Pedro Lobito Apr 28 '14 at 22:37
  • I think the explanation section is good, but OP's question is why doesn't `[^b]` work. That isn't addressed in the explanation. – Sam Apr 28 '14 at 22:38
11

^[^b] works.

The ^ OUTSIDE the []s indicates "the beginning of the string"

Douglas Denhartog
  • 2,036
  • 1
  • 16
  • 23
11

[^b] will only match one character that is not 'b'.
[^b]+ will specify that RegEx group to match one or more characters that are not 'b'.
[^b]* will specify that RegEx group to match zero or more characters that are not 'b'.

dgp
  • 953
  • 1
  • 8
  • 11
5

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.

Sam
  • 20,096
  • 2
  • 45
  • 71
0

You can use this with inputFormatter in flutter language

This will exclude emoji and this special character. If you want to add number also you can put into this.

 inputFormatter: <TextInputFormatter>[
          FilteringTextInputFormatter.deny(r'[¬¦्«»ΩΠμ§¡¿±·£%,!#$%&*+";:/=?^`{|}~₹)(•√π÷×¶∆€¥¢°©®™✓><\[\]\\’|‘|”|“|]\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff]')
]
Dilip
  • 2,622
  • 1
  • 20
  • 27