简体   繁体   中英

Java: Obtain matched string from an input

I am trying to obtain the string that my matcher is able to find using my provided expression. Something like this..

if(matcher.find())
    System.out.println("Matched string is: " + ?);

What would be the appropriate code for this? According to Oracle the

matcher.group();

method returns only the provided input same as

matcher.group(0);

Thanks in advance..

Edit:

Example follows below:

private static String fileExtensionPattern = ".*<input type=\"hidden\" name=\".*\" value=\".*\" />.*";
private static Matcher fileXtensionMatcher;
private static String input = text  "<html><body><table width="96"><tr><td><img src=&quot;file:/test&quot;  /><input type="hidden" name="docExt" value=".doc" />Employee Trv Log 2011 Training Trip.doc</td></tr></table></body></html>"

private static void findFileExtension() {
    System.out.println("** Searching for file extension **");
    System.out.println("Looking for pattern: " + fileExtensionPattern);
    fileXtensionMatcher = fileXtensionExp.matcher(input);

    if(fileXtensionMatcher.find()) {
        //the extension expression is contained in the string
        System.out.println("Extension expression found.");
        System.out.println(fileXtensionMatcher.group());
    }
}

The obtained result is:

text    "<html><body><table width="96"><tr><td><img src=&quot;file:/test&quot;  /><input type="hidden" name="docExt" value=".doc" />Employee Trv Log 2011 Training Trip.doc</td></tr></table></body></html>"

Why do you think that group() returns the input?

According to the JavaDoc :

Returns the input subsequence matched by the previous match.

In other words: it returns that part of the input that was matched.

After you added the source code, I can assure you the group() returns the whole input string because it matches your regular expression. If you want just the <input> element use:

private static String fileExtensionPattern = "<input type=\"hidden\" name=\".*\" value=\".*\" />";

Or use:

private static String fileExtensionPattern = ".*(<input type=\"hidden\" name=\".*\" value=\".*\" />).*";
. . .
System.out.println(fileXtensionMatcher.group(1));

After seeing your update it seems like you need matcher groups. Also you need to make your matches non-greedy ( .*? instead of .* ). Try this:

private static String fileExtensionPattern = 
    ".*<input type=\"hidden\" name=\".*?\" value=\"(.*?)\" />([^<]*)";

// etc.
private static void findFileExtension() {

     // etc.
     if(fileXtensionMatcher.find()) {
        // etc.
        System.out.println(fileXtensionMatcher.group(1));
        System.out.println(fileXtensionMatcher.group(2));
    }
}

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