2013-01-15 54 views
1

我想用Pattern & Matcher使用模式檢索某些文字和匹配器

<form name="loginForm" id="loginForm" method="post" onsubmit="ScrollUp(60);return validateLoginForm();" 
       enctype="multipart/form-data" action="/login.php"> 
       <input type="hidden" name="Rpidci" value=""> 
       <div class="last_box"> 
        <div class="second_box_heading_panel"> 
         <h1>Existing users - 
          <span> Login here</span> 
         </h1> 
        </div> 
        <div class="second_box_form_panel"> 
         <div class="error-msg"> 
                 </div> 
         <div class="name_form_panel"> 
          <div class="name">User Name 
          </div> 
          <div class="name_text_field"> 
           <input name="sHZnGSgdzmIJoKWOCHmYez" type="text" class="existing_user round_four" id="sHZnGSgdzmIJoKWOCHmYez" maxlength="10" value=""/> 
          </div> 
         </div> 
         <div class="name_form_panel"> 
          <div class="name">Password 
          </div> 
          <div class="name_text_field"><input name="AWrPDfe" type="password" class="existing_user round_four" id="AWrPDfe" maxlength = "20" 
          value=""/> 
          </div> 
         </div> 


           <div class="login_btn"><a href="javascript:void(0);" onclick="javascript:ScrollUp(70);return validateLoginForm();"><img src="images/login_btn.png" title="login here" /></a></div> 
          </div> 
          </div> 
         <div class="name_form_panel"></div> 

                 </div> 

        </div> 
       </form> 

我想找回這兩個字段的值來檢索網頁一些值

<input name="sHZnGSgdzmIJoKWOCHmYez" type="text" class="existing_user round_four" id="sHZnGSgdzmIJoKWOCHmYez" maxlength="10" value=""/>

&

<input name="AWrPDfe" type="password" class="existing_user round_four" id="AWrPDfe" maxlength = "20" value=""/>

我嘗試了幾次,但沒有得到輸出。請幫忙。

編輯:

我試過的代碼如下:(不一樣,我寫了一開始我很沮喪,並把事情搞糟了非常多)

Matcher matcher = Pattern.compile("<form name=\"loginForm\" .+ method=\"post\" .+ action=\"/login.php\">\\s*<input[^>]+>\\s*<input[^>]+>\\s*").matcher(loginResp); 

     String[] strArr = matcher.group(0).split("<input"); 
     String str1 = ""; 
     String str2 = ""; 
     String str3 = ""; 
     String str4 = ""; 

     Pattern localPattern = Pattern.compile(" name=\"([^\\s]+)\" type=\"text\" id=\"([^\\s]+)\" value=\"([^\\s]+)\" />"); 
     Matcher localMatcher2 = localPattern.matcher(strArr[3]); 
     if (localMatcher2.find()) { 
      str1 = localMatcher2.group(1); 
      echo("STR1 " + str1); 
      str2 = localMatcher2.group(3); 
      echo("STR2 " + str2); 
     } 
+0

你是[解析HTML cthulhu的方式](http://www.codinghorror.com/blog/2009/11/parsing-html-the-cthulhu-way.html) – jlordo

+0

嗯,公平地說,如果它是隻有這兩個值,HTML解析將是矯枉過正。 – Sentry

+0

如果只是這兩個值,HTML解析將是微不足道的,我懷疑他會發布問題regexps –

回答

2

與以往一樣,我會推薦使用HTML解析器,如JTidyJSoup。使用正則表達式無法可靠地執行此操作,並且HTML解析器是一種更簡單的解決方案。

+0

是的,正則表達式需要代碼始終保持完全一樣。 – Chris

+0

@BrianAgnew我不熟悉JTidy或Jsoup。 –

+0

我也不是!除了鏈接頁面記錄如何使用它們以及API看起來相對簡單的事實 –

0

您可以使用xpath查詢獲取這兩個字段的值而不是正則表達式。 Refer this link for xpath tutorial。

+0

如果您可以從該站點發布一些相關示例,將會更加欣賞,因爲之前我們已經看到答案被接受,但鏈接已經成爲死亡,所以有同樣問題的其他人不能收集答案。 – sadaf2605

相關問題