简体   繁体   English

解析特定 substring 的字符串?

[英]Parse string for specific substring?

I have a file with n amount of strings like this:我有一个包含 n 个字符串的文件,如下所示:

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我只需要解析 *.gif 和 HTTP 状态 - 304

My approach is either first split the string into string arrays and look at the exact index.我的方法是首先将字符串拆分为字符串 arrays 并查看确切的索引。 6 for the path, 8 for status code. 6为路径,8为状态码。 Then search 6 for.gif and copy the point from the last /.然后搜索6 for.gif,把最后一个/的那个点复制过来。 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.或者简单地在整个字符串中搜索 .gif,从 beginningng 到 the.gif 创建一个新的子字符串,然后从最后一个 / 它不干净。 Is there a regex expression for something like /*.gif that'll pick up logosmall.gif?是否有类似 /*.gif 的正则表达式可以提取 logosmall.gif?

Regex正则表达式

[^/]+\.gif

One or many characters that are not forward slash, followed by the literal ".gif"一个或多个不是正斜杠的字符,后跟文字“.gif”

This site offers interactive tutorials to learn regex https://regexone.com/lesson/introduction_abcs该站点提供交互式教程来学习正则表达式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.如果这不是您要查找的内容或您想了解更多信息,请对此答案发表评论或添加到您的问题中。

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 .有关所用正则表达式的说明,请将每个正则表达式(不带外引号)复制/粘贴到regex101.com中。

The Console Window will display:控制台 Window 会显示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM