gpt4 book ai didi

Java正则表达式匹配变量值

转载 作者:行者123 更新时间:2023-12-02 03:07:27 27 4
gpt4 key购买 nike

我正在解析包含代码的文本文件中提取特定变量的值。变量名称发生了变化,它在代码中的位置也发生了变化,但我知道模式,因此我成功获取了它的值并将其存储在名为 myVar 的变量中。

要获取值,即 myVar 和等号后面引号之间的字符串,即 myVar = "value i Want is here" 我考虑使用正则表达式如下:

Pattern q = Pattern.compile(myVar + "\= \"(.*)\"" );

但是编译时出现错误:

error: illegal escape character Pattern q = Pattern.compile(myVar + "\= \"(.*)\"" );

这与将 myVar 字符串与正则表达式连接有关吗?

最佳答案

\= 不是转义序列。您可能需要转义 java 的正则表达式字符串。这是执行此操作的链接: http://www.freeformatter.com/java-dotnet-escape.html

Escape Sequences
\t Insert a tab in the text at this point.
\b Insert a backspace in the text at this point.
\n Insert a newline in the text at this point.
\r Insert a carriage return in the text at this point.
\f Insert a formfeed in the text at this point.
\' Insert a single quote character in the text at this point.
\" Insert a double quote character in the text at this point.
\\ Insert a backslash character in the text at this point.

来自http://docs.oracle.com/javase/tutorial/java/data/characters.html

关于Java正则表达式匹配变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41557708/

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