Home  >  Article  >  Backend Development  >  Detailed introduction to Python's string matching

Detailed introduction to Python's string matching

高洛峰
高洛峰Original
2017-03-22 09:30:001320browse

This article mainly introduces Python to do simple string related information for detailed matching. Friends who need it can refer to

Do it in Python Detailed explanation of simple string matching

Since it is necessary to extract some fields in specific formats from semi-structured text data and assist in data mining and analysis work, Matlab tools have been used in the past for structured data processing. For modeling, matlab is good at matrix processing and structured data calculations. Python has the same characteristics as matlab: simple syntax and rich libraries. It is a simple and easy-to-use language for algorithm simulation.

It is relatively easy to get started with Python for string matching, and it has a mature string processing library re for us to use;

With the help of the re library, we only need to The matching work can be completed in two steps, which is much easier for workers doing data analysis/algorithms:

#step1: Construct the regular expression pattern, and use compile()FunctionProductionRegular expression object

step2: Call the methods and properties of the expression object generated by step1, and return the matching result

# 导入正则表达式匹配模块 Py 3.0 
import re 
text = "today is 01/04/2015, happy new year..." 
 
#建立日期的正则表达式 
detepat = re.compile('(\d+)/(\d+)/(\d+)') 
 
#进行匹配并打印结果 
result = detepat.finditer(text) 
for m in result: 
  print(m.group())

The above is the detailed content of Detailed introduction to Python's string matching. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn