• 主页
  • 系列总集
  • OpenCV
  • CMake
  • iOS
  • Java
  • 前端
所有文章 关于我

  • 主页
  • 系列总集
  • OpenCV
  • CMake
  • iOS
  • Java
  • 前端

Python三连(二):如何使用正则获取变量

2018-10-02

使用正则

Python里的正则库是, 使用的方式有两种

  1. 直接匹配 match
  2. 预编译匹配 compile
1
2
3
4
5
6
7
8
import re

string = '''<string name="TP40008091" title="CH100-SWW">The Move Away from Threads</string>'''

# 直接匹配
match = re.match("<string *name=\"(?P<key>.*?)\".*>(?P<value>.*)</string>", string)
# 预编译匹配
match = re.compile("<string *name=\"(?P<key>.*?)\".*>(?P<value>.*)</string>").match(string)

注意 直接匹配和预编译匹配返回对象的match函数的入参不一样

获取到变量

获取到变量主要是参考re的说明文档中

  • (?P…) The substring matched by the group is accessible by name.

返回的结果match是一个 Match() 的Object,为系统的自带class

1
2
3
4
5
6
7
8
match = re.compile("<string *name=\"(?P<key>.*?)\".*>(?P<value>.*)</string>").match(string)
key = match.group("key")
value = match.group("value")

print(" Key : %s \n Value : %s" % (key, value))
# Key : TP40008091
# Value : The Move Away from Threads

匹配多行

上述是对单一字符串的匹配,如果想要匹配多行(例如文件的内容),可以使用re中带有 flags=0 的方法flags参数

标记位 含义
A ASCII
I IGNORECASE
L LOCALE
M MULTILINE
S DOTALL
X VERBOSE
U UNICODE

使用的应该是 re.M 这个参数,不同的参数之间可以使用bit运算

1
2
3
4
5
6
7
8
9
10
11
12
13

content = '''
<string name="TP40000001" title="CH101-SWW">The Move</string>
<string name="TP40000002" title="CH102-SWW">The Move Away</string>
<string name="TP40000003" title="CH103-SWW">The Move Away from Threads</string>
'''

matches = re.compile("<string *name=\"(?P<key>.*?)\".*>(?P<value>.*)</string>", re.M | re.U).finditer(content)
for match in matches:
key = match.group("key")
value = match.group("value")
print(" Key : %s \n Value : %s" % (key, value))

贪婪匹配和非贪婪匹配

贪婪匹配和非贪婪匹配的原则主要在于回溯逻辑的不同,贪婪指的是尽可能匹配更多的字符,非贪婪则相反

1
2
3
4
5
6
7
8
9
10
11
string = '''<string name="TP40008091" title="CH100-SWW">The Move Away from Threads</string>'''

# 贪婪
# Key : TP40008091" title="CH100-SWW
# Value : The Move Away from Threads
greed = re.match("<string *name=\"(?P<key>.*?)\".*>(?P<value>.*)</string>", string)

# 非贪婪
# Key : TP40008091
# Value : The Move Away from Threads
simple = re.match("<string *name=\"(?P<key>.*)\".*>(?P<value>.*)</string>", string)

区别在于子表达式 (?P.*) 和 (?P.*?) 中最后问号的差别

pattern中的r什么意思

很多晚上的回答中,喜欢在pattern之前加入小写字母r,这个在python中代表 Raw 的意思,是字符串规则的一部分和正则规定无关

1
2
3
4
5
6
# 匹配
match = re.match("<string *name=\"(?P<key>.*?)\".*>(?P<value>.*)</string>", string)

# Raw 匹配
match = re.match(r"<string *name=\"(?P<key>.*?)\".*>(?P<value>.*)</string>", string)

区别在于StackOverflow答案中的举例

1
2
3
4
print('\n') # 换行
print(r'\n') # 字符串\n
print('\b') # 空格
print(r'\b') # 字符串\b
赏

请问老板还招人么(/ω\)

支付宝
微信
  • Python
  • Tips

扫一扫,分享到微信

微信分享二维码
Python三连(三):Json的读写
Python三连(一):脚本参数设定和解析
© 2021 Alan Li
Hexo Theme Yilia by Litten
  • 所有文章
  • 关于我

tag:

  • iOS
  • Java
  • Collection
  • Python
  • Shell
  • CMake
  • Memory
  • JavaScript
  • Architecture
  • AnchorPoint
  • Android
  • Web
  • Annotation
  • AFNetworking
  • Window
  • ViewController
  • AutoLayout
  • Dozer
  • CoreAnimation
  • Cycle Retain
  • Block
  • UI
  • IDE
  • FrontEnd
  • CSS
  • Category
  • TableViewCell
  • Security
  • Net
  • JSP
  • Spring
  • C
  • MyBatis
  • Date
  • React
  • GCD
  • UITouch
  • Gesture
  • UIControl
  • Git
  • HTML
  • HTTPS
  • HTTP
  • Servlet
  • Server
  • DataBase
  • MySQL
  • Linux
  • Tutorial
  • Ajax
  • Type
  • JQuery
  • JSON
  • Exception
  • Parameter
  • Reflect
  • Thread
  • Sort
  • KVO
  • MKMapKit
  • Overlay
  • Maven
  • Configure
  • Tips
  • Transaction
  • Swift
  • NavigationBar
  • Nginx
  • Runtime
  • OpenCV
  • Property
  • Playground
  • Protocol
  • Redux
  • ScrollView
  • Session
  • Cookie
  • Shiro
  • Error
  • Singleton
  • RegEx
  • StackView
  • StatusBar
  • Base64
  • Socket
  • TCP
  • IP
  • TextField
  • CALayer
  • UILabel
  • View
  • Animation
  • Xcode
  • Hexo
  • Terminal
  • OC
  • Device
  • Log
  • Image
  • JUnit
  • Oval
  • Archive
  • XSS
  • Compiler
  • Aspect
  • Responder
  • Class
  • FireWall
  • RetainCount
  • Const
  • Frame
  • String
  • Symbols
  • Framework
  • CocoaPods
  • Unity
  • Message
  • Button
  • AuthorizationStatus
  • Struct
  • XCTest
  • NSNotification
  • Contact

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

我写的,大概率是错的。。。。。