0

I would like to extract the first comment block of a CSS file which is like this :

/*
author : name
uri : link
etc
*/

without extracting the other comments

/* header */
/* footer */

So I tried this :

print re.findall(r'\/\*(.*)\*\/', cssText )

this gave me all the other comments except the block I need. so I changed it into this, to be more precise :

print re.findall(r'\/\*\n(.*)^\*\/', cssText )

and the result was nothing :

[]

Do you have suggestions? Thanks :-)

3 Answers 3

1

If you only need the first comment you can simply only use the first result:

print re.findall(r'\/\*(.*)\*\/', cssText )[0]

You can also use re.search which searches for the first matching occurrence:

print re.search(r'\/\*(.*)\*\/', cssText )
Sign up to request clarification or add additional context in comments.

1 Comment

Working with [0] gave me the first comments just after the block I need. The other one works with .group() and it give the same result as the first.
1

When you match multi line string, you need to make . match \n too:

print re.findall(r'\/\*\n(.*?)\*\/', cssText, re.S)

see: http://docs.python.org/2/library/re.html#re.S

1 Comment

Ups, I messed up quotation /* instead \*. Check now, it should work.
0

You can do this:

css = """
/*
author : name
uri : link
etc
*/

bla bla bla. Blah blah
x: 10;
color: red;

/* header */
/* footer */
"""

import re

pat = r'\/\*([\S\s]*?)\*\/'

print re.findall(pat, css)
print re.search(pat, css).group()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.