Replace non-ASCII with regular expressions in Python

This article uses Python3.

code

test.py


import re

text = '''For those who have never experienced setbacks,
A It means that I have never tried anything new.
ヲ abc123!=*Z
-Albert Einstein-'''
after = re.sub('[^!-~\\s]|[ ]', '○', text)

print (after)

Execution result

○○○○○○○○○○○○○○
○○○○○○○○0 1○○○○○○○○○○○○○○○○
○abc123!=*○
-○○○○○○○○○○○○○○○○-

reference

Recommended Posts

Replace non-ASCII with regular expressions in Python
Use regular expressions in Python
Handling regular expressions with PHP / Python
When using regular expressions in Python
[Python] Regular Expressions Regular Expressions
Overlapping regular expressions in Python and Java
Don't use \ d in Python 3 regular expressions!
How to use regular expressions in Python
Python: Simplified morphological analysis with regular expressions
Regular expression in Python
Regular expression in Python
Pharmaceutical company researchers summarized regular expressions in Python
[Python] Get rid of dating with regular expressions
Remove extra strings in URLs with regular expressions
Multiple regression expressions in Python
Scraping with selenium in Python
Scraping with chromedriver in python
Debugging with pdb in Python
Use regular expressions in C
Working with sounds in Python
Scraping with Selenium in Python
Scraping with Tor in Python
Tweet with image in Python
Wrap long expressions in python
Extract numbers with regular expressions
Combined with permutations in Python
Regular expression manipulation with Python
About Python and regular expressions
[Small story] [Python] Replace strings in 2D arrays with numbers
Get rid of dirty data with Python and regular expressions
Number recognition in images with Python
Testing with random numbers in Python
GOTO in Python with Sublime Text 3
Working with LibreOffice in Python: import
Replace dictionary value with Python> update ()
Scraping with Selenium in Python (Basic)
CSS parsing with cssutils in Python
I can't remember Python regular expressions
Numer0n with items made in Python
Open UTF-8 with BOM in Python
Use rospy with virtualenv in Python3
Use Python in pyenv with NeoVim
Heatmap with Dendrogram in Python + matplotlib
Read files in parallel with Python
Password generation in texto with python
Use OpenCV with Python 3 in Window
Until dealing with python in Atom
Get started with Python in Blender
Working with DICOM images in Python
A memo that handles double-byte double quotes in Python regular expressions
Regular expressions that are easy and solid to learn in Python
Ansible Jinja2 filters Replace and extract variable strings with regular expressions
When you want to replace multiple characters in a string without using regular expressions in python3 series
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Try logging in to qiita with Python
Stress Test with Locust written in Python
Python3> in keyword> True with partial match?
Device monitoring with On-box Python in IOS-XE
Try working with binary data in Python
Draw Nozomi Sasaki in Excel with python
Tips for dealing with binaries in Python