From: Νίκος on
On 7 Αύγ, 21:24, MRAB <pyt...(a)mrabarnett.plus.com> wrote:

> Use group capture:
>
>      found = re.match(r'<!-- (\d+) -->', firstline).group(1)
>      print(page_id)

Worked like a charm! Thanks a lot!

So match method here not only searched for the string representation
of the number but also convert it to integer as well?

r stand for retrieve the string here?

and group?

Wehn a regex searched a .txt file when is retrieving something for it
always retrieve it as string right? or can get it as a number as well?
From: Thomas Jollans on
On 08/07/2010 08:51 PM, Νίκος wrote:
> On 7 Αύγ, 21:24, MRAB <pyt...(a)mrabarnett.plus.com> wrote:
>
>> Use group capture:
>>
>> found = re.match(r'<!-- (\d+) -->', firstline).group(1)
>> print(page_id)
>
> Worked like a charm! Thanks a lot!
>
> So match method here not only searched for the string representation
> of the number but also convert it to integer as well?
>
> r stand for retrieve the string here?

r"xyz" is a raw string literal. That means that backslash escapes are
turned off -- r'\n' == '\\n'

>
> and group?
>
> Wehn a regex searched a .txt file when is retrieving something for it
> always retrieve it as string right? or can get it as a number as well?

From: MRAB on
Νίκος wrote:
> On 7 Αύγ, 21:27, MRAB <pyt...(a)mrabarnett.plus.com> wrote:
>> Νίκος wrote:
>>> i also dont know what wrong with this line:
>>> host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0]
>>> hostmatch = re.search('cyta', host)
>>> if cookie.has_key('visitor') != 'nikos' or hostmatch is None:
>>> # do stuff
>>> the 'stuff' never gets executed, while i want them to be as long as i
>>> dont have regex match!
>> Try printing out repr(host). Does it contain "cyta"?
>
> Yes it does contain it as print shown!
>
> is something wrong with this line in logic or syntax?
>
> if cookie.has_key('visitor') != 'nikos' or re.search('cyta', host) is
> None:
> # do database stuff
>
You said "i want them to be as long as i dont have regex match".

re.search('cyta', host) will return None if there's no match, but you
said "Yes it does contain it", so there _is_ a match, therefore:

hostmatch is None

is False.

From: MRAB on
Νίκος wrote:
> On 7 Αύγ, 21:24, MRAB <pyt...(a)mrabarnett.plus.com> wrote:
>
>> Use group capture:
>>
>> found = re.match(r'<!-- (\d+) -->', firstline).group(1)
>> print(page_id)
>
> Worked like a charm! Thanks a lot!
>
> So match method here not only searched for the string representation
> of the number but also convert it to integer as well?
>
> r stand for retrieve the string here?
>
> and group?
>
> Wehn a regex searched a .txt file when is retrieving something for it
> always retrieve it as string right? or can get it as a number as well?

The 'r' prefix makes it a 'raw string literal'. That means that the
string literal won't treat backslashes as special. Before raw string
literals were added to the Python language I would have needed to write:

'<!-- (\\d+) -->'

instead.

(Actually, that's not strictly true in this case, because \d doesn't
have a special meaning Python strings, but it's a good idea to use raw
string literals habitually when writing regexes in order to reduce the
chance of forgetting them when they _are_ necessary. Well, that's what I
think, anyway. :-))
From: Νίκος on
On 7 Αύγ, 22:07, MRAB <pyt...(a)mrabarnett.plus.com> wrote:

> re.search('cyta', host) will return None if there's no match, but you
> said "Yes it does contain it", so there _is_ a match, therefore:
>
>      hostmatch is None
>
> is False.

The code block inside the if structure must be executes ONLY if the
'visitor' cookie is not set to the client's browser or the hostname
address of the client doesn't contain in it the string 'cyta'.

# ======================================
# do not increment the counter if a Cookie is set to the visitors
browser already
# ======================================

if cookie.has_key('visitor') != 'nikos' or re.search('cyta', host) is
None:

I still don't get it :)