简体   繁体   English

在python中使用正则表达式开始字符

[英]Using beginning regex characters in python

I'm trying to implement regular expressions in python so that I will only print part of a string and not a whole string. 我正在尝试在python中实现正则表达式,以便只打印一部分字符串,而不是整个字符串。 I'm trying to print the characters that are numbers and also precede the letter characters of this string. 我正在尝试打印数字字符,并且也要在此字符串的字母字符之前打印。 I'm getting an error with the beginning of string character here 我在这里出现字符串字符开头错误

Heres my code import re 继承人我的代码导入重新

var = "20cw"
var2 = re.compile(^[0-9]?[0-9], var)
print var2

Here is the error I'm getting 这是我遇到的错误

  File "./temp.py", line 5
    var2 = re.compile(^[0-9]?[0-9], var)
                      ^
SyntaxError: invalid syntax

Expected output is 预期输出为

20

将正则表达式用字符串括起来(带引号):

var2 = re.findall('^\d{2}', var)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM