Question

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 :-)

Was it helpful?

Solution 3

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()

OTHER TIPS

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 )

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top