简体   繁体   中英

Regex OR expression in Python

I am new to regex expressions and to python. How can I write a script in python where it finds multiple spaces or tabs and replace them with single space .. how do I write or expression?

I wrote the line below for multiple spaces , how do i include the tab too?

ModCon = re.sub('\s{2,}', ' ', content)

To OR characters, you can just use a character class:

content = re.sub("[ \t]{2,}", " ", content)

You can OR arbitrary expressions with parentheses and vertical bar:

content = re.sub("( |\t){2,}", " ", content)

Try this

ModCon = re.sub('(\ |\t)+', ' ', content)

If there is one or more spaces or tabs, it becomes only one

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM