Java RegEx no match found error

Following regex giving me java.lang.IllegalStateException: No match found error

String requestpattern = "^[A-Za-z]+ \\/+(\\w+)";
Pattern p = Pattern.compile(requestpattern);
Matcher matcher = p.matcher(requeststring);
return matcher.group(1);

where request string is

POST //upload/sendData.htm HTTP/1.1

Any help would be appreciated.

Answers


No match has been attempted. Call find() before calling group().

public static void main(String[] args) {
    String requeststring = "POST //upload/sendData.htm HTTP/1.1";
    String requestpattern = "^[A-Za-z]+ \\/+(\\w+)";
    Pattern p = Pattern.compile(requestpattern);
    Matcher matcher = p.matcher(requeststring);
    System.out.println(matcher.find());
    System.out.println(matcher.group(1));
}

Output:

true
upload

Need Your Help

How well do PHP and Oracle fit together in an enterprise setting?

php oracle

We use Oracle and PHP together with some success. It seems there are a lot of rough edges as compared to, for instance, Java + Oracle. I am familiar with the Underground PHP + Oracle guide and that

Sonar Violation: Security - Array is stored directly - Why only arrays?

java sonarqube

I have seen the link before asking question here, I wonder why SONAR complaining only arrays and not custom objects.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.