SoFunction
Updated on 2024-11-10

Python to do simple string matching details

Python to do simple string matching details

Due to the need to extract some specific format fields in semi-structured text data, data-assisted mining analysis work, in the past are using Matlab tools for structured data processing modeling, matlab is good at matrix processing, structured data computation, Python has a common feature with matlab: concise syntax, rich libraries, for algorithmic simulation are a concise Python has common features with matlab: simple syntax, rich libraries, and is a simple language for algorithm simulation.

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

With the help of re library, the matching can be done in two simple steps, much easier for workers doing data analysis/algorithms:

step1: build regular expression pattern and use compile() function to produce regular expression object

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

<span style="color:#333300;"># Import Regular Expression Matching Module Py 3.0
import re 
text = "today is 01/04/2015, happy new year..." 
 
# Create regular expressions for dates
detepat = ('(\d+)/(\d+)/(\d+)') 
 
# Make matches and print results
result = (text) 
for m in result: 
  print(())</span> 

Thanks for reading, I hope this helps, and thanks for supporting this site!