0

In java file I read jsp file and trying to finding out number of css clasess used, "class=" and its value by using regex below.

Pattern p = Pattern.compile("class=\"([^\"]*)\"");
Set set = new HashSet();
Iterator iterator;
while ((strLine = br.readLine()) != null)
{
    Matcher m = p.matcher(strLine);
}
while (m.find())
{
    String classValue = m.group(1);
    set.add(classValue);
}

it gives me class name means if jsp contents class="List" or class="listItem".

Output is { List listItem } like this. My problem is as follows if my JSP contents

  1. then it will shows com.metaparadigm.jsonrpc.JSONRPCBridge that i dont want
  2. "> in this it will give me output = "<%=w_canEdit?" but i want only one class either IconSpacing or IconDisable how can do this
Toto
  • 89,455
  • 62
  • 89
  • 125
Vaishali
  • 11
  • 1
  • 5
  • 1
    This is quite unreadable. Please reformat your question; pasting source code, highlighting it and then pressing Ctrl-K formats it as such. – Tim Pietzcker Jan 18 '12 at 07:50
  • 2
    I tried to improve your post, but I have no idea what the last paragraph wants to tell me. Please improve this Paragraph. If you click on the edit link you will find help on the formatting on the right of the window. – stema Jan 18 '12 at 08:00

1 Answers1

0

Question

From your encrypted description assuming I have deciphered it correctly !

Looks to me that your jsp page contains following line

<img src="a.jpeg" class="<%=w_canEdit?"IconSpacing":"IconDisable"%>"/>

And your regular expression matches <%=w_canEdit?\

@Test
public void testRegex() {

    Pattern p = Pattern.compile("class=\"([^\"]*)\"");
    Set set = new HashSet();


    //<img class="<%=w_canEdit?"IconSpacing":"IconDisable"%>" src="a.jpeg"/>
    String str="<img src=\"a.jpeg\" class=\"<%=w_canEdit?\"IconSpacing\":\"IconDisable\"%>\"/>";
    System.out.println(str);


    Matcher m = p.matcher(str);
    while (m.find())
    {
        String classValue = m.group(1);
        set.add(classValue);
    }
            System.out.println("Result:");
    System.out.println(set);
}

Output

Input:
<img src="a.jpeg" class="<%=w_canEdit?"IconSpacing":"IconDisable"%>"/>
Result:
[<%=w_canEdit?]

what you expect in the result

[IconSpacing,IconDisable]

Answer

Short answer :

you can not do it with regex

Long answer :

you can not do it with regex, even if with lookahead hacks you might be able to resolve it as <%=w_canEdit?"IconSpacing":"IconDisable"%> like for ex using following pattern

Pattern p = Pattern.compile("class=\"(<%=(.(?<!%>\"))*)\"");
// [<%=w_canEdit?"IconSpacing":"IconDisable"%>]

you will still never be able to identify runtime value of a class [as either IconSpacing or IconDisable ] by parsing jsp file anyways.

Easiest way to do this is to to do it manually

  1. grep class= *.jsp
  2. identify css classes that have jsp scriptlets in them
  3. extract the required details from the result

If you can raise a separate Question with exact details of your requirement people here would be definitely happy to help


Also See this post RegEx match open tags except XHTML self-contained tags to understand why using regex to parse html/jsp pages is not that great idea !

Community
  • 1
  • 1
Prashant Bhate
  • 10,907
  • 7
  • 47
  • 82