1

This is my code

>>> ll = 'window.DETAILS_PAGE_MAP_GLOBALS = {GOOGLE_MAPS_LONGITUDE: 55.2378015294,GOOGLE_MAPS_LATITUDE: 25.0463764816}'
>>> print(re.match('GOOGLE_MAPS_LATITUDE:\s*(\d+\.\d+)', ll))
None

I always get None though I am sure 100% that the regular expression is correct.

could you help please?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Marco Dinatsoli
  • 10,322
  • 37
  • 139
  • 253
  • It is not duplicated guys. I didn't know that I have to use search instead of match. please be more careful about these things. consider that not all users are professionals in python – Marco Dinatsoli May 27 '14 at 20:54

2 Answers2

1

Use re.search(), not re.match(); the latter only matches at the start of the string:

If zero or more characters at the beginning of string match the regular expression pattern, return a corresponding MatchObject instance.

Demo:

>>> import re
>>> ll = 'window.DETAILS_PAGE_MAP_GLOBALS = {GOOGLE_MAPS_LONGITUDE: 55.2378015294,GOOGLE_MAPS_LATITUDE: 25.0463764816}'
>>> re.match('GOOGLE_MAPS_LATITUDE:\s*(\d+\.\d+)', ll)
>>> re.search('GOOGLE_MAPS_LATITUDE:\s*(\d+\.\d+)', ll)
<_sre.SRE_Match object at 0x105525cd8>
>>> re.search('GOOGLE_MAPS_LATITUDE:\s*(\d+\.\d+)', ll).group(1)
'25.0463764816'
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

You need to use re.search() instead of re.match(). The first one is looking to the pattern anywhere in the string, and the other one looks if the pattern can be applied to the string exactly.

From the documentation:

re.search(pattern, string, flags=0)

Scan through string looking for a location where the regular expression pattern produces a match, and return a corresponding match object. Return None if no position in the string matches the pattern; note that this is different from finding a zero-length match at some point in the string.

Example:

>>> print(re.search('GOOGLE_MAPS_LATITUDE:\s*(\d+\.\d+)', ll))
<_sre.SRE_Match object at 0xffecf260>
>>> print(re.search('GOOGLE_MAPS_LATITUDE:\s*(\d+\.\d+)', ll).groups())
('25.0463764816',)
>>> print(re.search('GOOGLE_MAPS_LATITUDE:\s*(\d+\.\d+)', ll).group(1))
25.0463764816
Community
  • 1
  • 1
Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
  • what is this comma? and can I get the value without the `(` and the `)` please? – Marco Dinatsoli May 27 '14 at 20:49
  • Yes, you can. See my answer updated. Make sure to check if the `re.search()` object returned is not `None` before trying to call `group()` method. – Maxime Lorant May 27 '14 at 20:50
  • Many thanks, will accept it once the system allows +1 – Marco Dinatsoli May 27 '14 at 20:51
  • For the record, the second line returns a tuple: https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences – Maxime Lorant May 27 '14 at 20:51
  • is this question really a duplicated one? I didn't know that `search` could help. – Marco Dinatsoli May 27 '14 at 20:54
  • It is kind of a duplicate but it is hard to say for me it is one, since you didn't know the existence of the function (even though, a quick look in the documentation would have solved your problem). I would not have flag it as duplicate with this question. – Maxime Lorant May 27 '14 at 20:57
  • I already looked at the documentation and really tried the `search` but I got a strange results ( I am sure you know that results). So, I didn't think that search is good in my case – Marco Dinatsoli May 27 '14 at 20:58