Python according to regular expression to extract the specified content examples

@This article comes from the public number: csdn2299, if you like, you can pay attention to the public number programmer academy.
This article mainly introduces the relevant information of Python to extract the specified content examples
according to the regular expression. Designated content

Regular expressions are extremely powerful, and it is very convenient to use regular expressions to extract the desired content.
The following demonstrates the use of regular expressions in Python to extract content that meets the requirements.
Example code:

import re 
#   正则表达式是极其强大的,利用正则表达式来提取想要的内容是很方便的事。 
# 下面演示了在python里,通过正则表达式来提取符合要求的内容。有几个要注意 
# 的地方就是: 
# [1] 要用()将需要的内容包含起来 
# [2] 编号为0的group是整个符合正则表达式的内容,编号为1的是第一个(及对应 
#   的)包含的内容 
# @param regex: regular expression, use () to group the result 
#   正则表达式,用()将要提取的内容包含起来 
# @param content:  
# @param index: start from 1, depends on the \p regex's () 
#   从1开始,可以通过数(来得到,其中0是全部匹配 
# @return: the first match of the \p regex 
#   只返回第一次匹配的内容 
def extractData(regex, content, index=1): 
  r = '0'
  p = re.compile(regex) 
  m = p.search(content) 
  if m: 
    r = m.group(index) 
  return r 
  
regex = r'第(.*)场雪'
content = '2002年的第一场雪'
index = 1
print extractData(regex, content, index) 

Thank you very much for reading
. When I chose to study python at university, I found that I ate a bad computer foundation. I did n’t have an academic qualification. This is
nothing to do. I can only make up for it, so I started my own counterattack outside of coding. The road, continue to learn the core knowledge of python, in-depth study of computer basics, sorted out, if you are not willing to be mediocre, then join me in coding, and grow!
In fact, there are not only technology here, but also things beyond those technologies. For example, how to be an exquisite programmer, rather than "cock silk", the programmer itself is a noble existence, is it not? [Click to join] Want to be yourself, want to be a noble person, come on!

Published 54 original articles · Like 22 · Visits 30,000+

Guess you like

Origin blog.csdn.net/chengxun03/article/details/105567816