: Re.sub (R ' \s ', Lambda m: ' [' + m.group (0) + '] ', text, 0); "Replace the space in the string with ' [] '.
Re.split
You can use Re.split to split strings, such as: Re.split (R ' \s+ ', text), and split the string into a single word list.
Re.findall
Re.findall can get all the matching strings in the string. such as: Re.findall (R ' \w*oo\w* ', text); Gets all the words in the string that contain ' oo '
Python re Regular Expression instance code, pythonre Regular Expression
This article focuses on the reregex expressions of python. The details are as follows.
Concept: Regular Expressions (generic formulas) are expressions used to express a set of strings in a concise manner. The advantage is conciseness.
Application: string matching.
Instance code:
CODEC =
expressions in programming languages, when we want to match \ The time needs to match 4 \, \\\\ match \, because the first programming language will transfer \\\\ to \ \, and then the second time will be transferred \ \. If you use the native string r of Python to write the regular, you can write less two \, that is, R ' \ \ ' matches \,r ' \\d ' match ' \d ', R ' \d ' matches the numberUse of the 1.2 re m
Re is the most common regular expression module in Python, and the common methods include compile,match,findall,finditer,search,split,sub and so on.In some cases where the string itself is not easy to use, the RE module makes it easy to do some finding and replacing.1, compilePre-compiled regular expressions can save t
This article mainly introduces the details about join and split in Python. This article also uses an example to introduce the use of pythonjoin and split methods, for more information, see The python join and split methods. join i
press ' a ' to split ' and ' BCD ', then ' and ' BCD ' separately by ' B ' splitPrint (Re.split (' [ab] ', ' ABCD '))Replace the Re.sub () module1. Do not add parameters by default replace AllPrint (Re.sub (' A ', ' a ', ' Abcdeaedaer ', 1)) #AbcdeaedaerReplace SUBN (output can show the total number of replacements)Print (RE.SUBN (' A ', ' a ', ' Abcdeaedaer '))Re.complie () moduleCan be custom compiled, easy to call directlyObj=re.compile (' \d{2} '
Python intercepts the string using the variable [header subscript: tail subscript], you can intercept the corresponding string, where the subscript is starting from 0, can be positive or negative, subscript can be empty to take the head or tail.
# example 1: string intercept str = ' 12345678 ' Print str[0:1]>> 1 # output str position 0 start to position 1 characters before print Str[1:6] >> 23456 # Ou
This article mainly introduces the Python strip () function and the split () function of the details and examples of relevant information, the need for friends can refer to the following
A detailed and practical example of the Python strip () function and the split () funct
directory name is given, the output path and the empty file name
Second, examples1. Common examples1>>> U ="www.doiido.com.cn"2 3 #Use default delimiter4>>>PrintU.split ()5['www.doiido.com.cn']6 7 #with "." As a delimiter8>>>PrintU.split ('.')9['www','Doiido','com','cn']Ten One #Split 0 Times A>>>PrintU.split ('.', 0) -['www.doiido.com.cn'] - the #Split once ->>>PrintU.split ('.', 1) -['www'
A Brief introduction:In essence, a regular expression (or RE) is a small, highly specialized programming language,(in Python) it is embedded in Python and is implemented through the RE module. The regular expression pattern isCompiled into a sequence of bytecode, which is then executed by a matching engine written in C
single character matches, and matches all specified characters in ""., match only one character, with only one, can represent any character? , match one character, optional*, matches any character, is optional and does not match just one^, match the beginning of the line, if in [^] is expressed as a non-() grouping matches, matching characters within (), in group units+, match one or more, have and must have a|, when the range is matched or manipulated,Print(Re.findall ('compan (?: y|ies)','Too
Method_descriptor:split (...) S.split ([Sep [, Maxsplit]]), List of strings Return A list of the words in the string S, using Sep as the Deli Miter string. If Maxsplit is given, at most maxsplit splits be done. If Sep is no specified or is None, any whitespace string is a separator and empty strings be removed from the re Sult. (END)The string is split, the space or null character is
return a replacement string to be used. """ return _compile (pattern, flags). Sub (repl, string, count)Sub (pattern, REPL, String, count=0, flags=0)5. Split (pattern,string,maxsplit=0,flags=0)根据正则匹配分割字符串def Split (pattern, String, maxsplit=0, flags=0): "" "split the source string by the Occurrences of the pattern, returning a list containing the resu
One, what is reThe Python re module (Regular expression regex) provides matching operations for a variety of regular expressions, using this Python-embedded language tool, although not all complex matches can be met. But enough, in most cases, to effectively analyze complex strings and extract relevant information. Python
the edge (beginning or end) are in the delete sequence.
For example,
>>> a = ' 123' >>> a ' 123' >>> a.strip() '123'
(2) Here, the rm Delete sequence is deleted as long as the characters on the edge (beginning or end) are in the delete sequence.
For example,
>>> a = '123abc' >>> a.strip('21') '3abc' >>> a.strip('12') '3abc'
The results are the same.
2. Introduction to the
Python Basics of REThere are a couple of ways to use the RE module mainly:Re.match () # match a string from scratch Re.search () # Browse all strings to match the first rule-compliant string Re.findall () # place all matching items in a single list # Re.finditer () re.split () re.sub ( )well, Let's briefly introduce the basics of the RE module:1. meta-characters.
pattern match, the regular expression in Python is greedy pattern matching, it will meet the requirements of the entire expression, as much as possible to match the characters, the concrete effect is shown in the following example2.4 borders
Grammar
Description
Pattern Example
Match
^prev
Matches a string (caret) that begins with prev . In multiple li
This article mainly describes the Python Cookbook (string and text) for any number of separators to split the string operation, in conjunction with an instance of the analysis of Python using split () and regular expressions for string splitting operation related implementation skills, the need for friends can refer to
() # The match will be successful. else: print not match 'bt = 'bat | bit | bot 'match_object = re. search (bt, 'aabatsdf ') if match_object is not None: print "| search |" + match_object.group () # The match is successful. if match is used, the match will not succeed. else: print 'not Match'
The test environment of this example is Python2.7.6.
The running result is as follows:
I hope this article will he
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.