简体   繁体   中英

Is there a simple Java Regex (*SKIP)(*F) alternative?

You must match the content you want to avoid and use a capture group to extract what you want (I don't think there is an other way)<\/em> . A convenient pattern to do that can be:

(?:[^\w"]+|"[^"]*")*+(\w+)

These verbs are a quite useful way to tell the regex engine (PCRE in this case) that you want to discard those matches.

".*"|([^\W]+)
or
".*"|(\w+)

Unfortunately I can't yet comment on other posts, but Federico Piazza's solution will fail if there are multiple sets of quotes. For example if your text was the following:

String text = "test1 \"hello world!\" test2 \"foobar\" test3";

You want words outside of quotes with excluding trailing spaces:

[^"\s]++((?=\s*"[^\s])|(?=\s*$)|(?=[^"]+\s+"))

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