-2

I am using java....

.MainNav a:hover{ float:left; width:70px; height:65px; border-top: 2px Solid #F4E6CC; border-bottom: 2px Solid #805822; border-left: 2px Solid #F4E6CC; border-right: 2px Solid #805822; margin: 0px 0px 0px 0px; align:center; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; font-weight: bold; color: #FFFFFF; text-decoration: none; text-align: center; background:#C99349; background-image: url(../../images/hor_nav_bg.gif); background-repeat: repeat-X; padding:4px; clear:left; }

above is css class i want regular expression such that group contains values like

group1 = MainNav a:hover

group2 = { float:left; width:70px; height:65px; border-top: 2px Solid #F4E6CC; border-bottom: 2px Solid #805822; border-left: 2px Solid #F4E6CC; border-right: 2px Solid #805822; margin: 0px 0px 0px 0px; align:center; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; font-weight: bold; color: #FFFFFF; text-decoration: none; text-align: center; background:#C99349; background-image: url(../../images/hor_nav_bg.gif); background-repeat: repeat-X; padding:4px; clear:left; }

means class name and other is definition can you please tell me the regular expression for that? I am little bit confuse how to create expression for that so that I can get the output.

Here is my code for

package com.tufan.digite.Count;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GetAllCssFile {
    public static void main(String args[]) throws IOException {
        try {
            FileInputStream fstream = new FileInputStream("D:/digite/work/digite/WEBUI/common/theme1/common.css"); // Get theobject of DataInputStream
            DataInputStream dis = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(dis));
            String strLine;
            while ((strLine = br.readLine()) != null) {
                Matcher matcher = Pattern.compile("([^}]^)({[^}]+})", Pattern.DOTALL | Pattern.MULTILINE).matcher(strLine);
                if (matcher.find()) {
                    String selector = matcher.group(1);
                    String definition = matcher.group(2);
                    System.out.println("selector:" + selector + "definition:"+definition);
                }
            }
        } catch (Exception e) {
            //Do some exception handling here
        }
    }
}

it will not give any answers

Milad Naseri
  • 4,053
  • 1
  • 27
  • 39
Vaishali
  • 11
  • 1
  • 5
  • Realize that the `}` could also be placed inside a comment: `p { /* } */ color: #FFFFFF; }` – Bart Kiers Jan 19 '12 at 09:31
  • 1. PLEASE make your code's syntax work. Adding it as a single line simply won't do. I corrected that. 2. It's not advisable to use short form of words and abbreviations in the context of your question. – Milad Naseri Jan 20 '12 at 08:35
  • On a side note, you don't need to use a DataInputStream here (or practically anywhere else, for that matter). Just pass the FileInputStream directly to the InputStreamReader. – Alan Moore Jan 20 '12 at 11:18

1 Answers1

2

This should do the job, assuming you want it in JavaScript.

var regex = /([^\}\{]+)(\{[^\}]+\})/mi;

And this will work for Java:

Matcher matcher = Pattern.compile("([^\\}\\{]+)(\\{[^\\}]+\\})", Pattern.DOTALL | Pattern.MULTILINE).matcher(content);

Then invoking matcher.find(); would return the groups:

if (matcher.find()) {
    String selector = matcher.group(1);
    String definition = matcher.group(2);
}

The problem with your code is that you are matching the text line by line. That, of course, would not work.

Consider having a class like this:

public class CssDefinition {
    private String selector;
    private String definition;
    //Appropriate getters and setters go here.
}

Here is the correct version of your code:

public static void main(String args[]) throws IOException {
    FileInputStream fstream = null;
    try {
        fstream = new FileInputStream("D:/digite/work/digite/WEBUI/common/theme1/common.css"); // Get theobject of DataInputStream
        DataInputStream dis = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(dis));
        StringBuffer buffer = new StringBuffer();
        String strLine;
        while ((strLine = br.readLine()) != null) {
            buffer.append(strLine).append("\n");
        }
        String content = buffer.toString();
        content = content.replaceAll("(?s)/\\*.*?\\*/", ""); //This is to remove the comments from the CSS
        Matcher matcher = Pattern.compile("([^\\}\\{]+)(\\{[^\\}]+\\})", Pattern.DOTALL | Pattern.MULTILINE).matcher(content);
        Set<CssDefinition> definitions = new HashList<CssDefinitions>();
        while (matcher.find()) {
            CssDefinition definition = new CssDefinition();
            definition.setSelector(matcher.group(1));
            definition.setDefintion(matcher.group(2));
            definitions.add(definition);
        }
        //After this the set `definitions` will contain the extracted data
    } catch (Exception e) {
        //Do some exception handling here
    } finally {
        if (fstream != null) {
            fstream.close(); //Always release your resources
        }
    }
}

Also note that in the above code, I first remove all comments from your CSS. That's to make the code able to parse CSS codes like this correctly:

b {
    font-weight: bold; /* This is a {test} */
}

Since without removing the comments first it would give you:

String selector = "b";
String definition = "{\nfont-weight: bold; /* this is a {test}"

instead of:

String selector = "b";
String definition = "{\n\tfont-weight: bold; /* this is a {test}\n}"
Milad Naseri
  • 4,053
  • 1
  • 27
  • 39
  • I updated the answer. Also, do note that you had incorrectly removed the escaping `\` from before the `{` and `}` parentheses. Since they are special characters in the regular expression syntax, you should not remove the escaping. – Milad Naseri Jan 20 '12 at 08:51
  • but group(1) gives null blank value and group(2) gives correct value in braces. – Vaishali Jan 20 '12 at 11:19
  • That's because of a typographical error on my part in the regex definition. I corrected it. – Milad Naseri Jan 20 '12 at 11:30
  • Please don't do this: `(?:.|\\s)`; it's a recipe for [catastrophe](http://www.regular-expressions.info/catastrophic.html). (See [this answer](http://stackoverflow.com/a/2408599/20938) for an explanation.) Just use `.` in DOTALL mode. – Alan Moore Jan 20 '12 at 11:45
  • You are welcome :) if the answer has sufficient, you should mark it as an approved answer and close the question as solved. – Milad Naseri Jan 23 '12 at 07:06