“Python正则表达式1”的版本间的差异
来自CloudWiki
(创建页面,内容为“ <nowiki>import re line = "bobby123" #以b开头,重复任意多个字符 regex_str = "^b.*" if re.match(regex_str,line): print("yes") #以3结尾 regex_str2…”) |
|||
第1行: | 第1行: | ||
+ | |||
+ | [[文件:python10-20.png]] | ||
+ | |||
<nowiki>import re | <nowiki>import re | ||
2019年3月9日 (六) 07:08的最新版本
import re line = "bobby123" #以b开头,重复任意多个字符 regex_str = "^b.*" if re.match(regex_str,line): print("yes") #以3结尾 regex_str2 ="3$" if re.match(regex_str,line): print("yes") #从右侧向左,贪婪匹配 line2 ="boooooobby123" regex_str = ".*(b.*b).*" match_obj = re.match(regex_str,line2) if match_obj: print(match_obj.group(1)) #从左边开始,非贪婪匹配 line3 ="boooooobby123" regex_str = ".*?(b.*?b).*" match_obj = re.match(regex_str,line3) if match_obj: print(match_obj.group(1))