Python regex

Discussion in 'Python' started by Andrew Rekdal, Mar 13, 2008.

  1. 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
     
    Andrew Rekdal, Mar 13, 2008
    #1
    1. Advertising

  2. 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:...
    >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
    >
    >
     
    Andrew Rekdal, Mar 13, 2008
    #2
    1. Advertising

  3. 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 Delobelle, Mar 13, 2008
    #3
  4. --
    -- Andrew

    "Arnaud Delobelle" <> wrote in message
    news:...
    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
     
    Andrew Rekdal, Mar 13, 2008
    #4
  5. 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#SECTION005120000000000000000

    --
    Arnaud
     
    Arnaud Delobelle, Mar 13, 2008
    #5
  6. 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/cssutils/

    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
     
    Adonis Vargas, Mar 13, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmViQnVzaGVsbA==?=

    Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine?

    =?Utf-8?B?SmViQnVzaGVsbA==?=, Oct 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    714
    =?Utf-8?B?SmViQnVzaGVsbA==?=
    Oct 22, 2005
  2. Xah Lee
    Replies:
    1
    Views:
    946
    Ilias Lazaridis
    Sep 22, 2006
  3. Xah Lee
    Replies:
    8
    Views:
    463
    Ilias Lazaridis
    Sep 26, 2006
  4. Replies:
    3
    Views:
    773
    Reedick, Andrew
    Jul 1, 2008
  5. Xah Lee
    Replies:
    2
    Views:
    219
    Xah Lee
    Sep 25, 2006
Loading...

Share This Page