Using ^ to match beginning of line in Python regex

Posted on

Question :

Using ^ to match beginning of line in Python regex

I’m trying to extract publication years ISI-style data from the Thomson-Reuters Web of Science. The line for “Publication Year” looks like this (at the very beginning of a line):

PY 2015

For the script I’m writing I have defined the following regex function:

import re
f = open('savedrecs.txt')
wosrecords =

def findyears():
    result = re.findall(r'PY (dddd)', wosrecords)
    print result


This, however, gives false positive results because the pattern may appear elsewhere in the data.

So, I want to only match the pattern at the beginning of a line. Normally I would use ^ for this purpose, but r'^PY (dddd)' fails at matching my results. On the other hand, using n seems to do what I want, but that might lead to further complications for me.

Asked By: chrisk


Answer #1:

re.findall(r'^PY (dddd)', wosrecords, flags=re.MULTILINE)

should work, let me know if it doesn’t. I don’t have your data.

Answered By: sinhayash

Answer #2:

Use with re.M:

import re
p = re.compile(r'^PYs+(d{4})', re.M)
test_str = "PY123nPY 2015nPY 2017"
print(re.findall(p, test_str)) 

See IDEONE demo


  • ^ – Start of a line (due to re.M)
  • PY – Literal PY
  • s+ – 1 or more whitespace
  • (d{4}) – Capture group holding 4 digits
Answered By: Wiktor Stribi?ew

Answer #3:

In this particular case there is no need to use regular expressions, because the searched string is always ‘PY’ and is expected to be at the beginning of the line, so one can use string.find for this job. The find function returns the position the substring is found in the given string or line, so if it is found at the start of the string the returned value is 0 (-1 if not found at all), ie.:

In [12]: 'PY 2015'.find('PY')
Out[12]: 0

In [13]: ' PY 2015'.find('PY')
Out[13]: 1

Perhaps it could be a good idea to strip the white spaces, ie.:

In [14]: '  PY 2015'.find('PY')
Out[14]: 2

In [15]: '  PY 2015'.strip().find('PY')
Out[15]: 0

And next if only the year is of interest it can be extracted with split, ie.:

In [16]: '  PY 2015'.strip().split()[1]
Out[16]: '2015'
Answered By: mac13k

Leave a Reply

Your email address will not be published. Required fields are marked *