I’m using Java’s matcher to group terms in a string using the following regex:
Pattern.compile("(\\\\\"[^\\\\\"]*\\\\\"|[^\\s\\\\\"]+)");
This is the part I’m having trouble with: [^\s\\\”]
I’d like it to only match non-spaces and dangling escaped quotes such as \”. Is there any way to group the \\ and \” within a character class so they’re only matched together?
I tried to use lookahead/lookbehind, but found that including it within the character class put me back at square one.
A character class matches a single character. If I understood you correctly, you want to match only the string
\". To do this, you don’t need a character class at all–the regex\\"matches that already! (Inside a Java string, it would look like\\\\\"which is ridiculous, but there you have it.)You can group things together using parentheses:
(\\\\\"). You can also alternate inside a group like this using|. So to match non-spaces or\", you can do this:(\S|\\\\\"). (Note that\Sis the same as[^\s].)EDIT: I wasn’t paying enough attention. You can match everything but\"or a space as follows:(\\\\(?!")|[^\s\\]), I think.How about this:
([^\\s\\\\]|\\\\(?!")). This should match anything except whitespace or\or a\not followed by a".