This article talks about Python’s regular expressions.
No more nonsense, let’s start with the simplest one:
'.': can match any single character (just a dot) except line breaks.
'*' can match the previous subexpression zero or more times (just an asterisk).
So the combination of the above two '.*' (dot star) matches everything except newline characters.
'+': Repeat one or more times.
'?': Repeat zero or one time.
'\d': Matches a numeric character. Equivalent to [0-9].
'\w' matches any word character including an underscore. Equivalent to '[A-Za-z0-9_]'.
'/s' matches any whitespace character, including spaces, tabs, form feeds, etc. Equivalent to [ \f\n\r\t\v]
'^' matches the beginning of the input string.
'$' matches the end position of the input string.
The above are very commonly used, of course there are many more commonly used ones, please check the manual when needed.
This description is not intuitive enough, so let’s do the experiment directly. It is very simple to use regular expressions in Python, just import re directly:
>>> import re
>>>
First Try matching all:
>>> vlan = 'switchport access vlan 612' >>> ljds = re.search('.*',vlan).group() >>> ljds 'switchport access vlan 612'
Try matching numbers again:
>>> ljds = re.search('\d',vlan).group() >>> ljds '6'
Because '/d' matches a number, so if you want to match '612' here, three For numbers, you can add '{3}':
>>> ljds = re.search('\d{3}',vlan).group() >>> ljds
'612'
Similarly, if you want to match 13 characters (including spaces):
>>> ljds = re.search('[\w\s]{13}',vlan).group() >>> ljds 'switchport ac'
Here are also I would like to mention that the quantifiers of regular expressions involve greedy and non-greedy modes. Greedy is to take the maximum value and match as many matches as possible. Non-greedy is just the opposite (the default is greedy mode). For example:
The above is matching 13 characters. If it is written to match 2 to 10 characters, just write: '[\w\s]{2,10}', then what is matched is 2 One or 10? Because the default is greedy mode, it will match the maximum:
>>> ljds = re.search('[\w\s]{2,10}',vlan).group() >>> ljds 'switchport'
Add a question mark '?' after the quantifier to switch to non-greedy mode, that is, the minimum match:
>>> ljds = re.search('[\w\s]{2,10}?',vlan).group() >>> ljds 'sw'
Next, let’s introduce “capture”:
(exp): Match exp.
(?=exp): Match the position before exp.
(?
>>> vlan = 'switchport access vlan 612'
The most basic:
>>> ljds = re.search('(access)',vlan).group() >>> ljds 'access'
Match any character before 'access':
>>> ljds = re.search('.*(?=access)',vlan).group() >>> ljds 'switchport '
Matches any characters after 'vlan':
>>> ljds = re.search('(?<=vlan).*',vlan).group() >>> ljds ' 612'
OK, after learning this, look at the regular expression that captured the router name before:
DeviceName = re.search('.*(?=#show run)',telreply).group()
The above is the content of Python regular expression [1]. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Python is an interpreted language, but it also includes the compilation process. 1) Python code is first compiled into bytecode. 2) Bytecode is interpreted and executed by Python virtual machine. 3) This hybrid mechanism makes Python both flexible and efficient, but not as fast as a fully compiled language.

Useaforloopwheniteratingoverasequenceorforaspecificnumberoftimes;useawhileloopwhencontinuinguntilaconditionismet.Forloopsareidealforknownsequences,whilewhileloopssuitsituationswithundeterminediterations.

Pythonloopscanleadtoerrorslikeinfiniteloops,modifyinglistsduringiteration,off-by-oneerrors,zero-indexingissues,andnestedloopinefficiencies.Toavoidthese:1)Use'i

Forloopsareadvantageousforknowniterationsandsequences,offeringsimplicityandreadability;whileloopsareidealfordynamicconditionsandunknowniterations,providingcontrolovertermination.1)Forloopsareperfectforiteratingoverlists,tuples,orstrings,directlyacces

Pythonusesahybridmodelofcompilationandinterpretation:1)ThePythoninterpretercompilessourcecodeintoplatform-independentbytecode.2)ThePythonVirtualMachine(PVM)thenexecutesthisbytecode,balancingeaseofusewithperformance.

Pythonisbothinterpretedandcompiled.1)It'scompiledtobytecodeforportabilityacrossplatforms.2)Thebytecodeistheninterpreted,allowingfordynamictypingandrapiddevelopment,thoughitmaybeslowerthanfullycompiledlanguages.

Forloopsareidealwhenyouknowthenumberofiterationsinadvance,whilewhileloopsarebetterforsituationswhereyouneedtoloopuntilaconditionismet.Forloopsaremoreefficientandreadable,suitableforiteratingoversequences,whereaswhileloopsoffermorecontrolandareusefulf

Forloopsareusedwhenthenumberofiterationsisknowninadvance,whilewhileloopsareusedwhentheiterationsdependonacondition.1)Forloopsareidealforiteratingoversequenceslikelistsorarrays.2)Whileloopsaresuitableforscenarioswheretheloopcontinuesuntilaspecificcond


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
