The regular expression itself is a small, highly specialized programming language, the following article is mainly about Python using regular expressions to implement search words related data, the text gives a detailed example code, the need for friends can refer to, the following to see together.
Objective
In Python, Cheng can be called directly to implement a regular match by embedding the RE module inline. The regular expression pattern is compiled into a sequence of bytecode, which is then executed by a matching engine written in C.
For example, the following is used to find a word from a text, as follows:
Sample code
Import re pattern = ' this ' text = ' http://blog.csdn.net/caimouse are great, this is great way! ' Match = Re.search (pattern, text) s = Match.start () e = Match.end () print (' Found ' {} ' \nin ' {} ' \nfrom {} to {} ("{}" ) '. Format ( Match.re.pattern, match.string, S, E, text[s:e]))
The resulting output is as follows:
Found "This" in "Http://blog.csdn.net/caimouse are great, this is great way!" From-to-("this")
Here you use the start() start position that represents the match, and End () indicates the end position.
Summarize