[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Python regex

Timothy Adams" <teadams65

3/13/2008 8:04:00 PM

I hope posting is ok here for this question...

I am attempting to extract the text from a CSS comment using 're' such as...

string = "/* CSS comment /*"
exp = "[^(/*)].*[^(*/)] "

p = re.compile(exp)
q = p.search(string)
r = q.group()

print r

>>CSS comment

although this works to a degree... I know the within the brackets everything
is taken literally so the pattern
I am to negating is "(/*)". ie. includes the parenthesis.

So my question is...

Is there a way to negate a pattern that is more than on character long? eg.
where rather than saying if forward slash OR astrisk appear..negate.

I would be saying if parenthesis AND asterisk appear in this order... negate


-- Andrew


5 Answers

Timothy Adams" <teadams65

3/13/2008 8:09:00 PM

0

made error on last line... read as...

> I would be saying if forward-slash AND asterisk appear in this order...
> negate


--
-- Andrew

"Andrew Rekdal @comcast.net>" <<nospam> wrote in message
news:qLKdnR6s8a4xFUTanZ2dnUVZ_qmlnZ2d@comcast.com...
>I hope posting is ok here for this question...
>
> I am attempting to extract the text from a CSS comment using 're' such
> as...
>
> string = "/* CSS comment /*"
> exp = "[^(/*)].*[^(*/)] "
>
> p = re.compile(exp)
> q = p.search(string)
> r = q.group()
>
> print r
>
>>>CSS comment
>
> although this works to a degree... I know the within the brackets
> everything is taken literally so the pattern
> I am to negating is "(/*)". ie. includes the parenthesis.
>
> So my question is...
>
> Is there a way to negate a pattern that is more than on character long?
> eg. where rather than saying if forward slash OR astrisk appear..negate.
>
> I would be saying if parenthesis AND asterisk appear in this order...
> negate
>
>
> -- Andrew
>
>


Arnaud Delobelle

3/13/2008 8:14:00 PM

0

On Mar 13, 8:03 pm, "Andrew Rekdal" <<nospam>@comcast.net> wrote:
> I hope posting is ok here for this question...
>
> I am attempting to extract the text from a CSS comment using 're' such as....
>
> string = "/* CSS comment /*"
> exp = "[^(/*)].*[^(*/)] "
>
> p = re.compile(exp)
> q = p.search(string)
> r = q.group()
>
> print r
>
> >>CSS comment
>
> although this works to a degree... I know the within the brackets everything
> is taken literally so the pattern
> I am to negating is "(/*)". ie. includes the parenthesis.
>
> So my question is...
>
> Is there a way to negate a pattern that is more than on character long? eg.
> where rather than saying if forward slash OR astrisk appear..negate.
>
> I would be saying if parenthesis AND asterisk appear in this order... negate
>
> -- Andrew

There would be many ways to do this. One:

>>> import re
>>> r = re.compile(r'/\*(.*?)\*/')
>>> tst = '.a { color: 0xAACC66; /* Fav color */ }'
>>> m = r.search(tst)
>>> m.group(1)
' Fav color '
>>>

HTH

--
Arnaud

Timothy Adams" <teadams65

3/13/2008 8:23:00 PM

0



--
-- Andrew

"Arnaud Delobelle" <arnodel@googlemail.com> wrote in message
news:9e9cfd5e-72e6-4033-bc9f-b089ec8fd95c@i29g2000prf.googlegroups.com...
On Mar 13, 8:03 pm, "Andrew Rekdal" <<nospam>@comcast.net> wrote:
> I hope posting is ok here for this question...
>
> I am attempting to extract the text from a CSS comment using 're' such
> as...
>
> string = "/* CSS comment /*"
> exp = "[^(/*)].*[^(*/)] "
>
> p = re.compile(exp)
> q = p.search(string)
> r = q.group()
>
> print r
>
> >>CSS comment
>
> although this works to a degree... I know the within the brackets
> everything
> is taken literally so the pattern
> I am to negating is "(/*)". ie. includes the parenthesis.
>
> So my question is...
>
> Is there a way to negate a pattern that is more than on character long?
> eg.
> where rather than saying if forward slash OR astrisk appear..negate.
>
> I would be saying if parenthesis AND asterisk appear in this order...
> negate
>
> -- Andrew

There would be many ways to do this. One:

>>> import re
>>> r = re.compile(r'/\*(.*?)\*/')
>>> tst = '.a { color: 0xAACC66; /* Fav color */ }'
>>> m = r.search(tst)
>>> m.group(1)
' Fav color '
>>>

HTH

--
Arnaud

Arnaud,

in your expression above..

>>> r = re.compile(r'/\*(.*?)\*/')

what does the 'r' do?

-- andrew


Arnaud Delobelle

3/13/2008 8:31:00 PM

0

On Mar 13, 8:22 pm, "Andrew Rekdal" <<nospam>@comcast.net> wrote:
[...]
> in your expression above..
>
> >>> r = re.compile(r'/\*(.*?)\*/')
>
> what does the 'r' do?

It means the literal is a 'raw string' :

>>> print 'Hi\nthere!'
Hi
there!
>>> print r'Hi\nthere!'
Hi\nthere!
>>>

If you haven't done so already, I suggest reading the tutorial. Here
is a link to the relevant section on strings:

http://docs.python.org/tut/node5.html#SECTION00512000000...

--
Arnaud

Adonis Vargas

3/13/2008 11:46:00 PM

0

Andrew Rekdal < wrote:
> I hope posting is ok here for this question...
>
> I am attempting to extract the text from a CSS comment using 're' such as...
>
> string = "/* CSS comment /*"
> exp = "[^(/*)].*[^(*/)] "
>
> p = re.compile(exp)
> q = p.search(string)
> r = q.group()
>
> print r
>
>>> CSS comment
>
> although this works to a degree... I know the within the brackets everything
> is taken literally so the pattern
> I am to negating is "(/*)". ie. includes the parenthesis.
>
> So my question is...
>
> Is there a way to negate a pattern that is more than on character long? eg.
> where rather than saying if forward slash OR astrisk appear..negate.
>
> I would be saying if parenthesis AND asterisk appear in this order... negate
>
>
> -- Andrew
>
>

Have you looked into this library:

http://cthedot.de...

May help you, if you are trying to achieve something. If your doing it
as an exercise then I can not help you, I avoid regex like the plague
(but thats just me).

Hope this helps.

Adonis Vargas