gpt4 book ai didi

java - 从字符串中提取目录

转载 作者:行者123 更新时间:2023-11-30 06:10:20 26 4
gpt4 key购买 nike

我需要提取一个字符串的目录,例子如下:

222.77.201.211 - - [20/Sep/2013:00:10:23 +0800] "GET /mapreduce-nextgen/hadoop-internals-mapreduce-reference/ HTTP/1.1" 200 28664 "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;)"
220.181.89.164 - - [20/Sep/2013:00:10:25 +0800] "GET /mapreduce/hadoop-capacity-scheduler HTTP/1.1" 301 390 "-" "Sogou web spider/4.0(+http://www.sogou.com/docs/help/webmasters.htm#07)"
175.44.54.185 - - [20/Sep/2013:00:10:25 +0800] "GET /mapreduce-nextgen/apache-hadoop-2-0-3-published HTTP/1.1" 301 439 "http://dongxicheng.org/mapreduce-nextgen/apache-hadoop-2-0-3-published/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;)"
175.44.54.185 - - [20/Sep/2013:00:10:25 +0800] "GET /search-engine/scribe-intro/ HTTP/1.1" 200 21578 "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;)"
112.111.174.38 - - [20/Sep/2013:00:10:30 +0800] "GET /structure/segment-tree HTTP/1.1" 301 414 "http://dongxicheng.org/structure/segment-tree/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;)"
112.111.174.38 - - [20/Sep/2013:00:10:30 +0800] "GET /structure/segment-tree HTTP/1.1" 301 414 "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;)"
222.77.201.211 - - [20/Sep/2013:00:10:31 +0800] "GET /mapreduce-nextgen/apache-hadoop-2-0-3-published/ HTTP/1.1" 200 23438 "http://dongxicheng.org/mapreduce-nextgen/apache-hadoop-2-0-3-published/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;)"

预期的输出是:

  • /mapreduce-nextgen/hadoop-internals-mapreduce-reference/
  • /mapreduce/hadoop-capacity-scheduler
  • /mapreduce-nextgen/apache-hadoop-2-0-3-published
  • 等...

我认为可能需要正则表达式。提前致谢!

最佳答案

如果它总是在 GETHTTP 之间,最简单的 Regex 就是这个:

GET (.*?) HTTP

在这里证明:Regex101

在Java中,代码应该像下面这样:

Pattern p = Pattern.compile("GET (.*?) HTTP");
Matcher m = p.matcher(string);

编辑:不要忘记在字符串中的每个 " 之前放置 \,否则它将被解释为字符串的结尾。

String str = "222.77.201.211 - - [20/Sep/2013:00:10:23 +0800] \"GET /mapreduce-nextgen/hadoop-internals-mapreduce-reference/ HTTP/1.1\" 200 28664 \"-\" \"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;)\"";

您使用上述字符串的输出将是 /mapreduce-nextgen/hadoop-internals-mapreduce-reference/

关于java - 从字符串中提取目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35923477/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com