Use raw string to escape backslash character in case there is Escape Sequence.

Example: “c:\yaser\now\rule” ‘\n’ and ‘\r’ will be consider as Escape Sequence. when using r”c:\yaser\now\rule” it will be escaped and the python representation will add extra backslash before each backslash. if you want to escape special character in regex don’t forgot to escape them.

Explain

  • r’[ab]’ means a or b
  • r’[0123456789]’ same as this range r’[0-9]‘. ASCII Table
  • r’[a-zA-Z]’ means any small letters or capital letters.
  • r’[^0-9]’ means any character that is not a digit.
  • r’^y’ means start of the string with character y. start of the string not the start of words in the string.
  • r’r$’ means end of the string with character r.
  • r’^y$’ means just single character y
  • r’.’ means any single character except newline character.
  • r'a*' it will look at the expression, for example a character, before it and will match that 0 or more times.

    • r'a*' match empty string, a or aaaaa.
    • r'.*' match empty character, y or abcd.
    • r'[0-9]*' match empty character, 4 or 2131.
    • r'^[0-9]*$' Exclusive digit.
    • r'^[0-9][0-9]*$' Exclude empty character. There should be one digit and maybe there would be digits after it.
  • r’+’ same as r'*', but it will match 1 or more times.

    • r'^[0-9][0-9]*$' same as r’1+$’
  • r’?’ same as r'*', but it will match 0 or 1 time.

  • r”

Function

re.search(regex, text)

Example

re.search(r'y', 'yes')

It will return object if there is result otherwise it will be None

Match

References:


  1. 0-9 [return]