简体   繁体   English

Java正则表达式查找双引号

[英]Java regex to find double quote

What is the regular expression to find double quotes at the beginning of a string in Java? 在Java字符串的开头查找双引号的正则表达式是什么?

For example, I have this code: 例如,我有以下代码:

if (allLexeme[allLexemeIter].matches("\""))

and that works for the string " this because there is a space after the double quotes, but does not work for the string "this 并且适用于字符串" this因为双引号后有一个空格,但不适用于字符串"this

if( someString.startsWith("\"") )

The match() method requires that the entire input string be matched by the regular expression. match()方法要求整个输入字符串由正则表达式匹配。 So the regular expression "\\"" can only match the character sequence " 因此,正则表达式"\\""只能匹配字符序列"

You can use the find() method, which will find the "next" occurrence of the regular expression (a Matcher is stateful, tracking its progress through the input). 您可以使用find()方法,该方法将查找正则表达式的“下一个”出现( Matcher是有状态的,通过输入跟踪其进度)。

Or, you can alter the regular expression to match the entire input and keep using the match() method. 或者,您可以更改正则表达式以匹配整个输入,并继续使用match()方法。 Something like this: "\\".*" . But that probably isn't what you want. It depends on what you plan to do with the matched group. 像这样: "\\".*" ,但这可能不是您想要的,这取决于您打算对匹配的组执行什么操作。

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

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