Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java在字符之间查找子字符串_Java_Regex_String_Substring - Fatal编程技术网

Java在字符之间查找子字符串

Java在字符之间查找子字符串,java,regex,string,substring,Java,Regex,String,Substring,我被卡住了。我使用这种格式读取字符串中的玩家姓名,如下所示: "[PLAYER_yourname]" 我已经试了几个小时,不知道如何只阅读“u”后面和“]”之前的部分来获得名称 能给我一些帮助吗?我玩了一些小弦乐,分裂,一些正则表达式,没有运气。谢谢!:) 顺便说一句:这个问题是不同的,如果我分开,我不知道如何停止在第二个括号,因为我有其他字符串线超过第二个括号。谢谢 您可以使用子字符串int x=str.indexOf(''.')提供找到''.'的字符,而int y=str.lastInde

我被卡住了。我使用这种格式读取字符串中的玩家姓名,如下所示:

"[PLAYER_yourname]"
我已经试了几个小时,不知道如何只阅读“u”后面和“]”之前的部分来获得名称

能给我一些帮助吗?我玩了一些小弦乐,分裂,一些正则表达式,没有运气。谢谢!:)


顺便说一句:这个问题是不同的,如果我分开,我不知道如何停止在第二个括号,因为我有其他字符串线超过第二个括号。谢谢

您可以使用子字符串
int x=str.indexOf(''.')
提供找到''.'的字符,而
int y=str.lastIndexOF(']')
提供找到']'的字符。然后您可以执行str.substring(x+1,y),这将为您提供从符号后面到单词末尾的字符串,不包括右括号。

您可以执行以下操作:

String s = "[PLAYER_yourname]";
String name = s.substring(s.indexOf("_") + 1, s.lastIndexOf("]"));
你可以这样做-

public static void main(String[] args) throws InterruptedException {
        String s = "[PLAYER_yourname]";
        System.out.println(s.split("[_\\]]")[1]);
    }
输出:yourname

尝试:


此解决方案使用Java正则表达式

String player = "[PLAYER_yourname]";
Pattern PLAYER_PATTERN = Pattern.compile("^\\[PLAYER_(.*?)]$");
Matcher matcher = PLAYER_PATTERN.matcher(player);
if (matcher.matches()) {
  System.out.println( matcher.group(1) );
}

// prints yourname


使用
regex
匹配器函数,您可以执行以下操作:

String s = "[PLAYER_yourname]";
String p = "\\[[A-Z]+_(.+)\\]";

Pattern r = Pattern.compile(p);
Matcher m = r.matcher(s);

if (m.find( ))
   System.out.println(m.group(1));
结果:

yourname
\[ matches the character [ literally

[A-Z]+ match a single character (case sensitive + between one and unlimited times)

_ matches the character _ literally

1st Capturing group (.+) matches any character (except newline)

\] matches the character ] literally
说明:

yourname
\[ matches the character [ literally

[A-Z]+ match a single character (case sensitive + between one and unlimited times)

_ matches the character _ literally

1st Capturing group (.+) matches any character (except newline)

\] matches the character ] literally

使用split方法string.split(“-”)可以显示您的努力吗?您可以使用的索引,然后在n-1@SaketMittal那不是他想要的。确切的输入是什么?谢谢!我没有想到使用它,因为我不知道如何使用。indexOf或它的用途。我很感激!如果此答案解决了您的问题,那么您应该将其作为正确答案。请记住,如果用户句柄/播放器名称包含“]”,则此选项无效,您可以使用str.length-1来解决此问题。同样,扎卡里在回答这个问题之前回答了这个问题,完全一样…@BrandonLing或use
lastIndexOf
我喜欢这个解决方案,因为它可以解决一般情况。是的,它来自。它没有Java正则表达式的风格,但仍然可以作为一个有用的沙盒。