开发者

Question Pattern/Matcher

I want to extract the value 5342test behind the name="buddyname" from a fieldset tag. But there are multiple fieldsets in the HTML code. Below the example of the string in the HTML.

<fieldset style="display:none"><input type="hidden" name="buddyname" value="5342test" /></fieldset>

I ha开发者_运维问答ve some difficulties to put in the different patterns in Pattern.compile and i just want the value 5342test displayed not the other results, could somebody please help? Thank you.

My code:

String stringToSearch = "5342test";

            Pattern pattern = Pattern.compile("(\\value=\\})");  
            Matcher m = pattern.matcher(stringToSearch);

            while (m.find())
            {
              // get the matching group
              String codeGroup = m.group(1);

              // print the group
              System.out.format("'%s'\n", codeGroup); // should be 5342test
            }


Use this pattern:

Pattern pattern = Pattern.compile("<input[^>]*?value\\s*?=\\s*?\\\"(.*?)\\\"");


Since you want the input values inside a fieldset tag, you can use this regex pattern.

Pattern pattern = Pattern.compile("<fieldset[^>]*>[^<]*<input.+?value\\s*=\\s*\\\"([^\\\"]*)\\\"");
Matcher matcher = pattern.matcher("<fieldset style=\"display:none\"><input type=\"hidden\" name=\"buddyname\" value=\"5342test\" /></fieldset>");
if (matcher.find()) 
    System.out.println(matcher.group(1)); //this prints 5342test
else
    System.out.println("Input html does not have a fieldset");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜