Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正在尝试使用startswith检查字符串_Python_Python 3.x_String_Re_Startswith - Fatal编程技术网

Python 正在尝试使用startswith检查字符串

Python 正在尝试使用startswith检查字符串,python,python-3.x,string,re,startswith,Python,Python 3.x,String,Re,Startswith,我试图使用startswith检查某个东西是否以符号“开头,这是因为您不使用正则表达式line.startswith(“的文档的哪一部分使您得出结论,它与正则表达式一起工作?re.match从字符串开头隐式匹配,因此不需要^ import re # Edit as @donkopotamus pointed well that match doesn't require '^' at the begining in_re = re.compile(r'<\w+') print(bool

我试图使用startswith检查某个东西是否以符号“开头,这是因为您不使用正则表达式
line.startswith(“的文档的哪一部分使您得出结论,它与正则表达式一起工作?
re.match
从字符串开头隐式匹配,因此不需要
^
import re

# Edit as @donkopotamus pointed well that match doesn't require '^' at the begining
in_re = re.compile(r'<\w+')

print(bool(in_re.match('<test true')))
print(bool(in_re.match('test false')))
print(bool(in_re.match('test <false 2')))
True
False
False