-1

I got some code and don't know the meaning of them. Please explain for me about this:

s = re.sub(r"([.!?])", r" \1", s)
s = re.sub(r"[^a-zA-Z.!?]+", r" ", s)
s = re.sub(r"\s+", r" ", s).strip()
mrJohnt4
  • 33
  • 3

1 Answers1

0
  1. Matches to every '.', '!', '?' in s and then adds ' ' before it.
  2. Matches to every character in s that is not in alphabet (upper and lower) or '.' '!' '?' and replaces it with ' '.
  3. Matches to every whitespace character in s. So this includes spaces, tabs, newlines, vertical tabs, etc. replaces them with ' ' and strips (i.e. removes) the whitespace characters (like ' '). This seems kind of redundant considering strip should already handle whitespace that is being replaced in the first place, so this should be equivalent to just stripping s in the first place.
rLevv
  • 498
  • 3
  • 12