

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- How not to match a character after repetition in Python Regex?
- How to add a specific character to any empty space in MySQL table values?
- Posix character classes p{Space} Java regex.
- Regex to ignore a specific character in MongoDB?
- Java regex program to add space between a number and word in Java.
- How to find capitalized words and add a character before that in a given sentence using JavaScript?
- How to manipulate CSS pseudo-elements ::before and ::after using jQuery?
- How to match any character using Java RegEx
- How to match a white space equivalent using Java RegEx?
- Before and After Puzzle in C++
- MySQL query to get all characters before a specific character hyphen
- How to match a non-white space equivalent using Java RegEx?
- How to match a non-word character using Java RegEx?
- How to replace multiple spaces in a string using a single space using Java regex?
- The ::before and ::after Pseudo-element in CSS
Advertisements