Note that there are some explanatory texts on larger screens.

plurals
  1. POJava regex pattern matcher - how to allow a choice?
    text
    copied!<p>In Java I have a block of code that processes an Apache web server log, and checks the URL extension type. It works well when the URL is in the format "/index.html", but occasionally the URL is "/", which breaks the code.</p> <p>The below code works fine, but if in the input line "/index.html" is changed to "/" then it will break because line 19 <code>(\\.\\S*)</code> checks for a dot followed by a few characters, but if the URL is "/" there is no dot for the regex to find.</p> <p>How can I rewrite line 19 <code>(\\.\\S*)</code> to allow for a choice of .extension or "/"?</p> <p>In other words: <br> if URL=index.html, then extension is .html <br> if URL=index.php, then extension is .php <br> if URL=/, then extension is "" <br></p> <pre><code>import java.util.regex.*; public class Test { public static void main(String[] args) { String log_input = "123.45.67.89 - - [27/Oct/2000:09:27:09 -0400] \"GET /index.html HTTP/1.0\" 200 10450 \"-\" \"Mozilla/4.6 [en] (X11; U; OpenBSD 2.8 i386; Nav)\""; //String log_input = "123.45.67.89 - - [27/Oct/2000:09:27:09 -0400] \"GET / HTTP/1.0\" 200 10450 \"-\" \"Mozilla/4.6 [en] (X11; U; OpenBSD 2.8 i386; Nav)\""; //step 1 - split log line Pattern p = Pattern.compile("^([\\d.]+) (\\S+) (\\S+) \\[([\\w:/]+\\s[+\\-]\\d{4})\\] \"(.+)\" (\\d{3}) (\\d+) \"([^\"]+)\" \"([^\"]+)\""); Matcher m = p.matcher(log_input); m.matches(); String request_ip = m.group(1); String request_resource = m.group(5); System.out.println("Input: " + m.group(5)); //step 2 - check file extension Pattern p2 = Pattern.compile(".* .*(\\.\\S*) .*"); Matcher m2 = p2.matcher(request_resource); m2.matches(); String request_resource_ext = m2.group(1); System.out.println("Extension: " + request_resource_ext); if(request_resource_ext.matches("\\.htm|\\.html|\\.php|^$")){ //^$ in case the URL is / which has no extension System.out.println("Write"); }else{ System.out.println("Do not write"); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload