
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to add space before and after specific character using regex in Python?
The following code shows how space is added before and after the pipe '|' character in given string.
Example
import re regex = r'\b[|:]\b' s = "abracadabra abraca|dabara | abra cadabra abra ca dabra abra ca dabra abra" print(re.sub(regex, ' \g<0> ', s))
Output
This gives the output
abracadabra abraca | dabara | abra cadabra abra ca dabra abra ca dabra abra
- Related Articles
- How to add a specific character to any empty space in MySQL table values?
- How not to match a character after repetition in Python Regex?\n\n
- How to add space (" ") after an element using :after selector in CSS?
- Add a character to a specific position in a string using Python
- Regex to ignore a specific character in MongoDB?
- Posix character classes p{Space} Java regex.
- How to find capitalized words and add a character before that in a given sentence using JavaScript?
- Java regex program to add space between a number and word in Java.
- How to match any character using Java RegEx
- How to match a white space equivalent using Java RegEx?
- How to add a character before each word in a cell in Excel
- How to match a non-white space equivalent using Java RegEx?
- How to manipulate CSS pseudo-elements ::before and ::after using jQuery?
- How to match a non-word character using Java RegEx?
- MySQL query to get all characters before a specific character hyphen

Advertisements