简体   繁体   中英

Removing CSS comments with Java

I'm trying to make a little program in Java, I need it to remove all the comments from a CSS file and put them in a jList and finally put the CSS file without comments in a jTextArea. It's kinda working, for example:

/* this is a comment */ and this is not

Works: 在此输入图像描述

Another example:

/*
This is a comment
*/
this is not

That one works too: 在此输入图像描述

But, the last one:

/*
w
w
*/

Won't work: 在此输入图像描述

The part of putting the comment in the jList works fine but on the jTextArea is showing the complete file/source...

I don't know what to do, this is getting a little frustrating, I was hopping you guys could give me a hand!

If you don't know some of the words on the program, here's a short translation list:

  • Archivo > File
  • Abrir > Open
  • Cerrar > Close
  • Compilar > Compile
  • Editor > Editor
  • Lexico > Lexic
  • Sintactico > Syntax
  • Semantico > Semantic
  • Comentarios > Comments
  • Fuente sin comentarios > Source without comments
  • Espacios en blanco > white spaces

This are the source files https://gist.github.com/3929988 (CompiladorGUI.java and regex.java).

I'd really appreciate any help! Thanks in advance.

PS: Sorry for my english. As you could see, spanish is my main language.

Do not try to parse CSS yourself. There are ready-to-use CSS parsers. Take a look on this discussion to start: Looking for a CSS Parser in java

Your regex doesn't match multiple lines. Change the value of the atribute patron to

private String patron = "/\\*((.|[\\n\\r])+)?\\*/";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM