简体   繁体   English

使用正则表达式获取第三个正斜杠(“ /”)之后的字符

[英]Get characters after the third forward slash (“/”) - using regex

I've been trying to get the substring, after the third forward slash (" / ") from a string. 我一直在尝试从字符串中获取第三个正斜杠(“ / ”)之后的子字符串。

http://www.google.com/search?q=Regular+Expressions while stopping before ? http://www.google.com/search?q=Regular+Expressions,而在此之前停止? and # if they are present in the string. 和#(如果它们存在于字符串中)。

I have the regex: 我有正则表达式:

Pattern regex = Pattern.compile(":\\/\\/[0-9a-zA-Z-\\.:]+(\\/)([^?#]*)$");

however it doesn't work with every string 但是它不适用于每个字符串

I also came up with the regex: 我还想出了正则表达式:

Pattern regex = Pattern.compile("(.*)?:\\/\\/[^#?]*);

however this one grabs everything before the third forward slash (" / "). 但是,此命令将抢占第三个正斜杠(“ / ”)之前的所有内容。

What am I doing wrong? 我究竟做错了什么? Thanks 谢谢

You could try 你可以试试

(?:.*?\/){3}([^\/?#]+)

or in java 或用Java

(?:.*?\\/){3}([^\\/?#]+)

(escaped backslashes). (转义的反斜杠)。

It matches anything up to, and including, a slash - three times. 匹配最多(包括斜杠)的任何内容-三次。 Then captures everything up to, not including, a slash, question mark or hash sign. 然后捕获一切( 包括斜杠,问号或井号)。

Result is in capture group 1. 结果在捕获组1中。

Check it out here at regex101 . 在regex101处检查

This regex will work in java : 此正则表达式将在java中运行:

    public static void main(String[] args) throws Exception {
    String s = "http://www.google.com/search?q=Regular+Expressions";
    String regex = "(?:.*?/){2}.*?(/\\w+)(\\?|#).*"; // Don't capture anything upto the 3rd "/" then capture everything until you get a "?" or a "#" and then don't capture the rest. Replace everything with the captured value
    String str = s.replaceAll(regex, "$1");
    System.out.println(str);
    String s2 = "https://www.google.com/hello?test#";
    String str2 = s2.replaceAll(regex, "$1");
    System.out.println(str2);

}

O/P : O / P:

/search
/hello

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

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