Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use regular expressions to extract Special Information in python

Shulou Source: shulou.com Published: 2022-06-02 11:17:39 10月01日 Update

How do I use regular expressions to extract special information in python? I believe that many inexperienced people are at a loss about this, so this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

1. Delete the Python comment in the string

Case study:

Import re time = "2020-01-01 # this is a date" num = re.sub (ringing dates, ", time) # separated by #, removing the following information print (" this time is: ", num)

Results:

This time is: 2020-01-01

2. Intercept the content before and after a symbol

Case 1:

Txt ='my phone number is: 131My 246-XXX19'a = txt.split (':') [0] # 0 indicates that the result before the symbol b = txt.split (':') [1] # [- 1] is the same as the result of [1], indicating the print after the symbol ("a") print ("b")

Results:

The result of an is: my phone is b, and the result is: 131b-246-XXX19

Case 2:

Txt = "I love python. I love python" text = re.sub (r'. * $', ", txt) # to. For separation,\ is the escape character, with the following. Distinguish print ("this intercept result is:", text)

Results:

The result of this interception is: I love python

3. Delete non-numeric strings

Case study:

Import re time = "2020-01-01 # this is a date" num = re.sub (r'\ Downs, ", time) print (" this time is: ", num)

Results:

The time is: 20200101

4. Keep only Chinese

Case study:

Reg = "[^\ u4e00 -\ u9fa5]" text = "all right! e We aa $$to start 34 learning-python!" Print (re.sub (reg,', text))

Results:

All right, let's start learning.

5. Keep only Chinese, uppercase and lowercase letters and Arabic numerals

Case study:

Reg = "[^ 0-9A-Za-z\ u4e00 -\ u9fa5]" text = "all right! we aa $$to start 34 learning-python!" Print (re.sub (reg,', text))

Results:

All right, E, aa, let's start 34 to learn python.

6. Remove uppercase and lowercase letters and numbers

Case study:

Import retxt= "Ayouleyang A ~ Youle-Yang" text = re.sub ('[a-zA-Z0-9]',', txt) print (text)

Results:

A ~ Youle-Yang 1

7. Remove a special character

Case list:

Import re txt ='A * a # excellent $Le. : Yang;: youle+'text = re.sub ('[,;; youle::.;, $* #] +', ", txt) # [] will be replaced, similar to replace () print (text)

Results:

An a you Leyang + 1

8. Keep English, numbers and special symbols

Case study:

Reg = "[^ 0-9A-Za-z\ u4e00 -]" # keep only English, numbers and -, u4e00 "-" is the symbol that needs to be retained txt = "all right! my number is 131e00!:" text = re.sub (reg,', txt) print (text)

Results:

131C-246-XXX19

After reading the above, have you mastered how to use regular expressions to extract special information in python? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Results cases symbols special characters numbers time learning information content music rules expressions size letters strings methods dates more phone Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Apple NVidia MariaDB Shulou Technology