-3

I am able to replace characters in strings using the Python re library, but I need to use the pyparsing library (or no library at all) instead. Is there another way to do this without re?

import re
string = re.sub(r'[\t\r\n]', '', string)
b00kgrrl
  • 559
  • 2
  • 9
  • 30
  • 2
    Tried chaining `replace` calls? `str.translate` can help, too. – Wiktor Stribiżew Jun 26 '18 at 13:15
  • I'll give it a shot, thanks – b00kgrrl Jun 26 '18 at 13:17
  • Essentially you'll need to design a finite state machine based on the regular expression, then go through each character of input and transition through the state machine accordingly. If the state machine is in a valid state after the final character is processed, the expression matches. Please consider if you actually want to process a regular expression or string replacements. – h0r53 Jun 26 '18 at 13:17
  • 1
    You can use `str.maketrans` and `str.translate` to remove all these characters at once. See [my answer here](https://stackoverflow.com/questions/41535571/how-to-explain-the-str-maketrans-function-in-python-3-6/41536036#41536036) for the ways to create a translation table. – Patrick Haugh Jun 26 '18 at 13:24
  • Thanks, I'll check it out – b00kgrrl Jun 26 '18 at 13:26

1 Answers1

4
string = "".join(string.splitlines()).replace('\t', '')
Sunitha
  • 11,777
  • 2
  • 20
  • 23