Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #76901

Re: string encoding regex problem

From Peter Otten <__peter__@web.de>
Subject Re: string encoding regex problem
Date 2014-08-23 23:13 +0200
Organization None
References <lsm8ic$j90$1@online.de> <roy-7B58DA.20484615082014@news.panix.com> <lsmeej$49n$1@online.de> <mailman.13048.1408179734.18130.python-list@python.org> <ltaujl$3kj$1@online.de>
Newsgroups comp.lang.python
Message-ID <mailman.13356.1408828401.18130.python-list@python.org> (permalink)

Show all headers | View raw


Philipp Kraus wrote:

> I have create a short script:
> 
> ---------
> #!/usr/bin/env python
> 
> import re, urllib2
> 
> 
> def URLReader(url) :
>     f = urllib2.urlopen(url)
>     data = f.read()
>     f.close()
>     return data
> 
> 
> print re.match( "\<small\ \>.*\<\/small\>",
> URLReader("http://sourceforge.net/projects/boost/") )
> ---------
> 
> Within the data the string "<small>boost_1_56_0.tar.gz</small>" should
> be machted, but I get always a None result on the re.match, re.search
> returns also a None.

>>> help(re.match)
Help on function match in module re:

match(pattern, string, flags=0)
    Try to apply the pattern at the start of the string, returning
    a match object, or None if no match was found.

As the string doesn't start with your regex re.match() is clearly wrong, but 
re.search() works for me:

>>> import re, urllib2
>>> 
>>> 
>>> def URLReader(url) :
...     f = urllib2.urlopen(url)
...     data = f.read()
...     f.close()
...     return data
... 
>>> data = URLReader("http://sourceforge.net/projects/boost/")
>>> re.search("\<small\ \>.*\<\/small\>", data)
<_sre.SRE_Match object at 0x7f282dd58718>
>>> _.group()
'<small >boost_1_56_pdf.7z</small>'


> I have tested the regex under http://regex101.com/ with the HTML code
> and on the page the regex is matched.
> 
> Can you help me please to fix the problem, I don't understand that the
> match returns None

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

string encoding regex problem Philipp Kraus <philipp.kraus@flashpixx.de> - 2014-08-16 02:27 +0200
  Re: string encoding regex problem Roy Smith <roy@panix.com> - 2014-08-15 20:48 -0400
    Re: string encoding regex problem Philipp Kraus <philipp.kraus@flashpixx.de> - 2014-08-16 04:08 +0200
      Re: string encoding regex problem Roy Smith <roy@panix.com> - 2014-08-15 22:14 -0400
      Re: string encoding regex problem Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-16 14:35 +1000
      Re: string encoding regex problem Peter Otten <__peter__@web.de> - 2014-08-16 11:01 +0200
        Re: string encoding regex problem Philipp Kraus <philipp.kraus@flashpixx.de> - 2014-08-23 22:46 +0200
          Re: string encoding regex problem Peter Otten <__peter__@web.de> - 2014-08-23 23:13 +0200

csiph-web