Python does simple string matching parsing

  • 2020-05-27 06:01:22
  • OfStack

Python does simple string matching parsing

Because of the need in semi-structured text data to extract 1 some specific format field, data mining analysis work support, past Matlab tools are used for structured data processing model, matlab matrix processing and structured data calculation, and matlab Python has a common characteristic: syntax is concise and rich library, for simulation algorithm is 1 language is concise and easy to use.

Python is relatively easy to get started doing string matching, and has a mature string processing library, re, at our disposal.

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

step1: builds the regular expression pattern and produces regular expression objects using the compile() function

step2: calls the methods and properties of the expression object generated by step1, and returns the matching result


<span style="color:#333300;">#  Import the regular expression matching module  Py 3.0 
import re 
text = "today is 01/04/2015, happy new year..." 
 
# Creates a regular expression for the date  
detepat = re.compile('(\d+)/(\d+)/(\d+)') 
 
# Match and print the result  
result = detepat.finditer(text) 
for m in result: 
  print(m.group())</span> 

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: