Sam
Sam

Reputation: 2341

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. 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

Upvotes: 0

Views: 47

Answers (1)

Uriel
Uriel

Reputation: 16204

Enclose the regex as string (with quotation marks):

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

Upvotes: 1

Related Questions