minte9
LearnRemember / REGEX



Negated class

The class [^ ] matches any character that isn't listed.
 
#!/bin/sh

: "If you use [^..] instead of [..], 
the class matches any character that isn't listed

For example [^1-6] 
matches a character that's not 1 through 6

Regex [^x] 
Doesn't mean 'match unless there is an x'
but rather 'match if there is something that is not x'

The ^ used here is the same as start-of-line caret
But the meaning is completely different

q[^u] matches Iraqi, Iraqian
q[^u] doesn't match Qantas, because it called for a lower case q
q[^u] doesn't match Iraq, because q is at the end 
newline \n character is not [^u]
"

TXT='Iraqi Iraqian miqra liquor qasida qintar qoph zaqqum Qantas Iraq'

echo $TXT | grep 'q[^u]' > result.txt
echo $TXT | grep 'q[^u]' --colour
 
Iraqi Iraqian miqra liquor qasida qintar qoph zaqqum Qantas Iraq

Javascript

 
/**
 * Negated class [^ ]
 */

s = 'Iraqi Iraqian miqra liquor qasida qintar qoph zaqqum Qantas Iraq'
p = /(\w+)?q[^u](\w+)?/g
m = s.match(p)

console.log(m) // Iraqi, Iraqian, miqra, qasida, qintar, qoph, zaqqum






Questions and answers




q[^u] matches Iraq

  • a) no
  • b) yes, q followed by a char not u

[^1-6] matches a character

  • a) not 1 or 6
  • b) not 1 through 6
  • c) starts with 1 or 6

What does [^x] mean?

  • a) Match unless there is an x
  • b) Match if there is something that is not x

Which one refers to the start of the line?

  • a) ^
  • b) [^..]


References