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/9/three.js/2.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_String - Fatal编程技术网

我需要从java中的字符串中提取一些变量

我需要从java中的字符串中提取一些变量,java,string,Java,String,假设我有String str=“Hello$everybody$,那么$you$all$”是多少 从上面的字符串中,我需要将值每个人,都,全部提取到一个列表中。注意:所有必需的值都以$开始和结束。我如何用java实现它 请帮忙 关于,使用 您将需要一个类似以下的模式: \$[\w]+\$ 使用 您将需要一个类似以下的模式: \$[\w]+\$ 看看Java中的Matcher类 Pattern p = Pattern.compile( "$(.*)$", Pattern.DOTALL); Ma

假设我有
String str=“Hello$everybody$,那么$you$all$”是多少
从上面的字符串中,我需要将值
每个人
全部
提取到一个列表中。注意:所有必需的值都以$开始和结束。我如何用java实现它

请帮忙

关于,

使用

您将需要一个类似以下的模式:

\$[\w]+\$
使用

您将需要一个类似以下的模式:

\$[\w]+\$

看看Java中的
Matcher

Pattern p = Pattern.compile( "$(.*)$", Pattern.DOTALL);
Matcher matcher = p.matcher(str);
while (matcher.find()) {
    // Get the matching string
    String match = matcher.group();
}

看看Java中的
Matcher

Pattern p = Pattern.compile( "$(.*)$", Pattern.DOTALL);
Matcher matcher = p.matcher(str);
while (matcher.find()) {
    // Get the matching string
    String match = matcher.group();
}

尝试完成本教程:-它应该会有所帮助<代码>\$(\w+)\$
就是诀窍。

尝试完成本教程:-它应该会有所帮助<代码>\$(\w+)\$就是诀窍。

工作代码:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HelloWorld {

    public static void main(String[] args) {
        Pattern p = Pattern.compile("\\$(\\w.*?)\\$");
        String s = "Hello $everybody$. How $are$ you $all$";

        Matcher m = p.matcher(s);
        while (m.find()) {
                System.out.println(m.group(1));
        }
    }

}
工作代码:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HelloWorld {

    public static void main(String[] args) {
        Pattern p = Pattern.compile("\\$(\\w.*?)\\$");
        String s = "Hello $everybody$. How $are$ you $all$";

        Matcher m = p.matcher(s);
        while (m.find()) {
                System.out.println(m.group(1));
        }
    }

}

(.*)
也将捕获
$
,因此
$one$$two$
将是一个变量-是否如预期的那样?此外,
$
可能应该转义。此外,量词应该是不情愿的(
*?
),而不是贪婪的。
(.*)
也会捕获
$
,因此
$one$$two$
将是一个变量-是否如预期的那样?此外,
$
可能应该转义。此外,量词应该不情愿(
*?
),而不是贪婪。这是正则表达式量词不情愿的唯一答案。在写这篇文章的时候,所有其他人都使用贪婪的量词。它工作得很好。谢谢大家提供的所有答案。@SweetDream检查一下,这是正则表达式量词不愿意给出的唯一答案。在写这篇文章的时候,所有其他人都使用贪婪的量词。它工作得很好。谢谢大家的回答。@SweetDream请检查,然后不要忘记使用不情愿的量词:
+?
@EmilLundberg
\w
[a-zA-Z\u 0-9]
,因此不匹配
$
,所以在
中没有必要使用不情愿的量词:
+?
@EmilLundberg
\w
[a-zA-Z_0-9]
,因此不匹配
$
,所以
中没有必要使用