简体   繁体   中英

finding specific values from file using java

I have JSP file with some code

<div id="ListDiv" **class="ListDiv"** style="overflow: auto; visibility: visible">
    <table width="100%" border="0" cellspacing="0" cellpadding="0"  align="center">
        <tr>
            <td>
                <table **class="List"** cellspacing="0" cellpadding="0">
                     <tr>
                        <td class="ColHeader"><%=I18N.get("KEY_LABEL_Sr_No", w_locTextObj)%>.</td>
                        <td class="ColHeader"><%=I18N.get("KEY_LABEL_Project", w_locTextObj)%></td>
                        <td class="ColHeader"><%=I18N.get("KEY_LABEL_Status", w_locTextObj)%></td>
                        <td class="ColHeader"><%=I18N.get("KEY_LABEL_Estimated_Hours", w_locTextObj)%></td>


...

like this i wanted to find "class=" string and want value of it "ListDiv" (class="ListDiv",class="List") how many time means there count with value of class how can i get it .

  1. I have to read file using java
  2. finding out "class=" string but how can i get value?

1) Open and read the file. I assume you can do that. Note that your JSP and the generated HTML might have different counts for those values, eg when iterating over some collection.

2) In that case you might try and use a regular expression like class="([^"]*)" which would return anything after the string class=" and before the next double quotes as group 1. Note that in Java strings you have to escape the double quotes in that expression.

Example:

Pattern p = Pattern.compile("class=\"([^\"]*)\"");
Matcher m = p.matcher(yourFileContent);

while( m.find() ) {
  String classValue = m.group( 1 );
  //do whatever you want
}

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