Home > Software engineering >  Parse string for specific substring?
Parse string for specific substring?

Time:05-10

I have a file with n amount of strings like this:

something.other.com - - [01/Jul/1995:00:00:12 -0400] "GET /images/logosmall.gif HTTP/1.0" 304 0  

I need to parse *.gif and HTTP status only - 304

My approach is either first split the string into string arrays and look at the exact index. 6 for the path, 8 for status code. Then search 6 for .gif and copy the point from the last /. Or simply search for .gif in the whole string, make a new subtring from the begininng until the .gif then get substring from the last / It's not clean. Is there a regex expression for something like /*.gif that'll pick up logosmall.gif?

CodePudding user response:

Regex

[^/] \.gif

One or many characters that are not forward slash, followed by the literal ".gif"

This site offers interactive tutorials to learn regex https://regexone.com/lesson/introduction_abcs

If this is not what you are looking for or you would like to know more, please comment on this answer or add to your question.

CodePudding user response:

For the String provided:

String a = "something.other.com - - [01/Jul/1995:00:00:12 -0400] \"GET /images/logosmall.gif HTTP/1.0\" 304 0";
String imageFile = a.replaceAll(".(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", "").replace("\"", "").split("\\s ")[1].replaceAll("/.*/", "");
String status = a.split("\"\\s*")[2].split("\\s ")[0];

System.out.println("File Name:   -> "   imageFile);
System.out.println("HTTP Status: -> "   status);

For an explanation of the regular expressions used, copy/paste each one (without the outer quotation marks) into regex101.com.

The Console Window will display:

File Name:   -> logosmall.gif
HTTP Status: -> 304
  • Related