0

When I am using re.search, I have some problem.

For example:

a = '<span class="chapternum">1 </span>abc,def.</span>' 

How can I search the number '1'? Or how to search by matching digit start with ">" and end with writespace?

I tried:

test = re.search('(^>)(\d+)(\s$)', a)
print test
>> []

It is fail to get the number "1"

fredtantini
  • 15,966
  • 8
  • 49
  • 55
Enoch
  • 13
  • 4

1 Answers1

0

^ and $ indicate the beginning and the end of the string. If you get rid of them you have your answer:

>>> test = re.search('(>)(\d+)(\s)', a)
>>> test.groups()
('>', '1', ' ')

Not sure that you need the first and last groups though (capturing with parenthesis):

>>> a = '<span class="chapternum">23 </span>abc,def.</span>' 
>>> test = re.search('>(\d+)\s', a)
>>> test.group(1)
'23'
fredtantini
  • 15,966
  • 8
  • 49
  • 55
  • Thanks for your quickly response. Although this can find out '1', I can't sure that all the digit will match group(1) in my hold program. Therefore, I am seeking a way to search only the result that like this pattern: >\d+\s. – Enoch Jul 09 '17 at 07:52
  • `test.group(1)` means 'the first group', the `\d+` part captures all digits. – fredtantini Jul 09 '17 at 08:52
  • Oh. I got it. Thanks a lot. – Enoch Jul 10 '17 at 01:48