1

I have a question about the positioning of the "do not include operator in regex in python" shown below

[^]

If I have the following expression

print(re.findall(r'^[^_][-\w\d]+[^:/)]$',x))

does it matter where i place [^:/)] or will it only exclude : and / at the end of the string since i placed it at the end

Aran-Fey
  • 30,995
  • 8
  • 80
  • 121
Aaron B
  • 49
  • 4

1 Answers1

1

With the $ at the end of your regular expression you've anchored the [^:/)] character group to only match at the end of the string. Any matches must end with [^:/)].

Sean
  • 1,239
  • 9
  • 16
  • I see, so if I did not include the beginning ^ and end $, the "do not include operator [^]" would not count the string regardless of where the : and / are, not only at the end? – Aaron B May 07 '18 at 00:39
  • 1
    No, @aaron. A character class, your `[^:/)]`, can appear any place an individual character can appear. The `^` modifier at the start of the character class simply inverts the class of characters matched. It does not affect where in the string the character class matches. In other words, `[:/)]` will match any of those three characters at the position where the character class appears. Whereas `[^:/)]` matches any character other than those three chars in the same position. – Kurtis Rader May 07 '18 at 02:44
  • It's for the web, but I know these docs are reliable: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions – Sean May 07 '18 at 02:52