简体   繁体   English

使用Java从文件中查找特定值

[英]finding specific values from file using java

I have JSP file with some code 我有一些代码的JSP文件

<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 . 像这样,我想找到“ class =”字符串并想要它的值"ListDiv" (class="ListDiv",class="List")多少时间意味着在那里需要用class的值来计数。

  1. I have to read file using java 我必须使用Java读取文件
  2. finding out "class=" string but how can i get value? 找出“ class =“字符串,但是我如何获得价值?

1) Open and read the file. 1)打开并读取文件。 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. 请注意,例如,在迭代某些集合时,您的JSP和生成的HTML可能会对这些值具有不同的计数。

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. 2)在这种情况下,您可以尝试使用诸如class="([^"]*)"的正则表达式,该正则表达式将在字符串class="和下一个双引号作为组1之前返回任何内容。请注意,在Java字符串中您必须在该表达式中转义双引号。

Example: 例:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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