0

Can anybody explain this piece of code

  1. What does ^%s mean? why is it used there?

  2. the second '%' . what does it mean and why is it used there

  3. what does string.printable mean?

    re.compile('[^%s]' % re.escape(string.printable))
    
Dr. Snoopy
  • 55,122
  • 7
  • 121
  • 140
  • Is this python? – user3783243 Oct 26 '21 at 18:36
  • yes it is python 2.6. – sudha nippani Oct 26 '21 at 18:37
  • #3 = https://www.geeksforgeeks.org/python-string-printable/. #1 I think you are asking in context of a character class, is that correct? If so the `^` means `not` and the next is a list of characters for the regex to not match. Perhaps it is supposed to escape anything but `%s`, not clear to me. I think it would not escape any `%` or `s` found – user3783243 Oct 26 '21 at 18:41
  • ...Python _[2.6](https://www.python.org/dev/peps/pep-0361/#release-schedule)?!_ That's extremely ancient and should absolutely not be used for learning Python in 2021 under any circumstances. Find a tutorial from the last decade. Python 3.10 is the latest version. – ChrisGPT was on strike Oct 26 '21 at 23:13
  • 1
    Oh no my bad! i meant python 3.8 (i confused it with the tf version i was using) – sudha nippani Oct 27 '21 at 06:27
  • @Chris - Perhaps a bit forceful, in principal? "under any circumstances" - unless the user's system is restricted for an old(er) version for a reason beyond practical resolution. – S3DEV Oct 27 '21 at 07:45
  • @S3DEV, I maintain my position. If OP were actually using Python 2.6 to learn the language they'd be doing themselves a huge disservice. Tools like repl.it bring many languages right to your browser. The only valid reason I can think of to use Python 2.6 today is if you're working on a very legacy project, and in that situation I still think upgrading (at _least_ to 2.7) is worthwhile in virtually all cases. But under no circumstances should somebody learn Python using Python 2.6 today. – ChrisGPT was on strike Oct 27 '21 at 11:16

1 Answers1

1

Half of this question is a matter of regex syntax, and the other half is python's string format syntax.

  1. In regular expressions, the ^ character when inside of [] brackets indicates negation. i.e. the opposite of what's matched in the brackets. See a more complete answer on that expression
  2. Both of the % symbols relate to python format string syntax, which interpolates a value within the string you're re.compileing. The %s indicates the str() representation of the "argument" to the format string, which is the term after the % character - i.e. re.escape(string.printable) Read the docs for more examples of format specifiers
  3. string.printable is a built-in method specifying "printable" ASCII characters - see the doc

In short, it looks like that line is compiling a regex pattern that matches anything except the printable ASCII characters in a string

BentHam
  • 36
  • 5
  • Excellent answer. Just to add, the `re.esacpe` function is used to add an [escape character](https://en.m.wikipedia.org/wiki/Escape_sequence) to those punctuation characters which require escaping, in order to be presented in the string accurately. – S3DEV Oct 26 '21 at 19:38
  • @BentHam thankyou for your responses! Extremely clear and helpful ! – sudha nippani Oct 27 '21 at 06:55
  • 1
    @S3DEV this clarification helped! thankyou !! – sudha nippani Oct 27 '21 at 06:56
  • @sudhanippani - My pleasure. For the benefit of BentHam's effort, please remember to help those who helped you, by **accepting the answer** (click the tick mark by the answer). – S3DEV Oct 27 '21 at 07:49
  • 2
    @S3DEV I just started using stack overflow for asking questions, so I genuinely missed it. Thankyou for letting me know! – sudha nippani Oct 28 '21 at 06:04