0

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: enter image description here

Another example:

/*
This is a comment
*/
this is not

That one works too: enter image description here

But, the last one:

/*
w
w
*/

Won't work: enter image description here

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.

kustomrtr
  • 1,437
  • 2
  • 11
  • 14

2 Answers2

1

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

Community
  • 1
  • 1
AlexR
  • 114,158
  • 16
  • 130
  • 208
  • Hey! Thanks for the help, that would definitely be the best way to do it, and if it was up to me, that would be my first choice. The thing is that this is kind of a project I have for the university and we're not allowed to use parsers, the class is about compilers, so we must code our own parser. Thanks! – kustomrtr Oct 22 '12 at 07:15
1

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

private String patron = "/\\*((.|[\\n\\r])+)?\\*/";
Daniel Pereira
  • 2,720
  • 2
  • 28
  • 40
  • Hey Daniel, thanks for your answer, that new pattern worked, well kinda, the source without comments IS showing in the text area as expected but the jList is now missing those comments: [screenshot](http://i.imgur.com/nikFy.png)!! This is crazy :P – kustomrtr Oct 22 '12 at 07:20
  • @kustomrtr I didn't notice that you had a group call on the regex. I edited it, now it should work. – Daniel Pereira Oct 22 '12 at 07:46