Why do you need a r' '- a literal in a module re. Is it necessary?



  • Module doculation https://docs.python.org/3/library/re.html There are examples of the use of patters, both in the ordinary line of "and specific r" - the line:

    import re
    m = re.search('(?<=abc)def', 'abcdef')
    m.group(0)
    

    and

    m = re.search(r'(?<=-)\w+', 'spam-egg')
    m.group(0)
    

    Are they different at work and when they need to be used?



  • The answer is at the top of the page:

    The solution is to use Python’s raw string notation for regular expression patterns; backslashes are not handled in any special way in a string literal prefixed with 'r'♪ So... r"\n" is a two-character string containing '\' and 'n', while "\n" is a one-character string containing a newline. Usually patterns will be expressed in Python code using this raw string notation.

    Short: r- lines do not need to be screened (e.g. \the usual. r"\n" It's two symbols-- \ and n"\n" - One symbol (translation of lines).




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2