简体   繁体   中英

Java regex to find double quote

What is the regular expression to find double quotes at the beginning of a string in 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

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

The match() method requires that the entire input string be matched by the regular expression. 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).

Or, you can alter the regular expression to match the entire input and keep using the match() method. Something like this: "\\".*" . But that probably isn't what you want. It depends on what you plan to do with the matched group.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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