python matches the beginning of a string with a regular expression and prints the example

  • 2020-05-19 05:07:16
  • OfStack

This example shows how python USES regular expressions to match the beginning of a string and print it. I will share it with you for your reference as follows:


import re
s="name=z1hangshan username=fff url=www.baidu.com password=ddd256"
s2="username=fff name=z1hangshan url=www.baidu.com password=ddd256"
#p=re.compile(r'((?:\s)name=(\S)+)')
p=re.compile(r'(^name=(\S)+)')
iter=p.finditer(s)
for m in iter:
  print "m", m.group()
iter2=p.finditer(s2)
for m2 in iter:
  print "m2", m2.group()
#m username=fff
#m2  Don't match 

PS: here are two more handy regular expression tools for you to use:

JavaScript regular expression online testing tool:
http://tools.ofstack.com/regex/javascript

Online regular expression generation tool:
http://tools.ofstack.com/regex/create_reg

More about Python related content to view this site project: the Python regular expression usage summary ", "Python data structure and algorithm tutorial", "Python Socket programming skills summary", "Python function using techniques", "Python string skills summary", "Python introduction and advanced tutorial" and "Python file and directory skills summary"

I hope this article is helpful to you Python programming.


Related articles: