1

I am trying to repalce multiple characters (\t, \n or \r) in a given string to only one occurrence of character which is pipeline | :

What is required :

Current string : '\t \r\nFoo\t Fooo\t Dog\t Foo\t \r\n\r\n'

Desired Output : ' |Foo| Fooo| Dog| Foo| '

Here is what i have tried so far :

import re
string = '\t \r\nFoo\t Fooo\t Dog\t Foo\t \r\n\r\n'
re.sub('\r|\n|\t', '|', string)

Here is the output i got '| ||Foo| Fooo| Dog| Foo| ||||' where pipeline is duplicate here.

1
  • 2
    Why not | |Foo| Fooo| Dog| Foo| | why the first and last disappear ? Commented Aug 3, 2021 at 17:43

2 Answers 2

3

You may use + symbol in the regex to replace sequence of that chars

import re

string = '\t \r\nFoo\t Fooo\t Dog\t Foo\t \r\n\r\n'
res = re.sub('[\r\n\t]+', '|', string)
print(res)  # | |Foo| Fooo| Dog| Foo| |

Regarding your requirements, the first and last pipe have no reason to not appear, so for now I keep that

Sign up to request clarification or add additional context in comments.

Comments

1

To get the desired output, you can replace the tabs and the newlines at the start and ^ at the end $ of the string with an empty string.

Then replace all other tabs and newlines with a pipe char |

Regex demo | Python demo

import re

string = '\t \r\nFoo\t Fooo\t Dog\t Foo\t \r\n\r\n'
result = re.sub(
    '^[\r\n\t]+|[\r\n\t]+$|([\r\n\t]+)',
    lambda x: '|' if x.group(1) else '',
    string
)
print(result)

Output

|Foo| Fooo| Dog| Foo|

2 Comments

That worked as well !, saved me couple of lines of code.
@IbraheemAyoup You want to keep the existing spaces right? Else you could do \s+ See regex101.com/r/hTaodo/1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.