1
regexp = re.compile('[A-Z]\\d{4}')
prefix = regexp.match("\"O1533_FOO\" INTEGER NOT NULL ,")

Prefix is None, but I expected it to be "O1533".

When I use the web based tool http://regexr.com/ it works.

http://regexr.com/3bag5

I tried to escape the \ with different numbers of \s, and I also tried delimiting /s. Could not get it to work.

Can some RegExp expert please fix my expression?

Maroun
  • 94,125
  • 30
  • 188
  • 241
Kurt
  • 4,477
  • 2
  • 26
  • 34

1 Answers1

7

Because you should replace match with search.

match tries to match the string from the beginning, and \"O1533_FOO\" INTEGER NOT NULL , doesn't begin with something that matches [A-Z], hence it fails.

See search() vs. match():

Python offers two different primitive operations based on regular expressions: re.match() checks for a match only at the beginning of the string, while re.search() checks for a match anywhere in the string (this is what Perl does by default).

Maroun
  • 94,125
  • 30
  • 188
  • 241