我希望在解析包含代码的文本文件中提取特定变量的值。变量名称会更改,其名称在代码中的位置也会更改,但是我知道模式,因此我成功获取了其值并将其存储在名为myVar的变量中。
为了获得值,即myVar和等号后的引号之间的字符串,即myVar = "value i want is here"
我考虑如下使用正则表达式:
Pattern q = Pattern.compile(myVar + "\= \"(.*)\"" );
但是编译时出现错误:
错误:非法转义字符Pattern q = Pattern.compile(myVar +“ \ = \”(。*)\“”“);
这与将myVar字符串与正则表达式连接在一起有关吗?
\=
不是转义序列。您可能需要转义Java的Regex字符串。这是执行此操作的链接: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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句