mttprvst13 mttprvst13 - 1 year ago 90
Java Question

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

I am making a program in Java, and have run into a slight problem using Regular Expressions. I want to capture everything not enclosed in quotes. I have a regex pattern for that, right here, but the issue is, it cannot be used in Java. It uses the

trick to skip over the
, and find anything else(using
), but as I said, it cannot be used in Java. I have another pattern that is close, but not quite what I need, right here. It will find everything that does not have quotes directly in front or behind it. The issue with that one, is that If I have something like this:
Test1 "Hello World!" Test2
, and will grab
. I do not want to get
, because it is in the quotes. What I want to know, is if it is even possible to do what I want, and how if so.

Answer Source

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

Java doesn't have these verbs but you can use the same approach on java without the verbs (*SKIP)(*F), and then capture the content you want... so you can use:


And then grab the content from the capturing group 1.

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

Pattern ptrn = Pattern.compile("\".*\"|([^\\W]+)");
Matcher m = ptrn.matcher(text);

while (m.find()) {
    if ( != null) {
        System.out.println("Text: ";

IDEOne Demo

This is well known as the discard technique, where you can discard all the patterns that aren't captured and focus on the one you want to capture, for instance, if you have:


This will match all the patterns but will conly capture the last one

Regular expression visualization

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download