3

I am looking to use a one line String.split() to pull out the 'name' in a query I am writting..

The Pattern + Matcher works like expected, but I am pulling my hair out trying to figure out why String.split() doesn't return a match!

public static void main(String[] asdf)
{
    final String queryText = "id <equals> `1` <AND> name <equals> `some name`";
    final String regex = "^(.*name <equals> `)([\\S\\s]*)(`.*)$";

    System.out.println("Splitting...");
    final String[] split = queryText.split(regex);
    for (int i = 0; i < split.length; i++)
    {
        System.out.println(split[i]);
    }

    System.out.println("Matching...");
    final Pattern pattern = Pattern.compile(regex);
    final Matcher matcher = pattern.matcher(queryText);

    if (matcher.find())
    {
        for (int i = 0; i < matcher.groupCount(); i++)
        {
            System.out.println(matcher.group(i + 1));
        }
    }
}

Prints the output

Splitting...
Matching...
id <equals> `1` <AND> name <equals> `
some name
`
5
  • 4
    Your match covers the entire string. Hence, if you split by the match, the two resulting strings are empty. And trailing empty strings are omitted by default. Use the [overload with an integer argument](docs.oracle.com/javase/1.4.2/docs/api/java/lang/…, int)) and supply a negative integer to get the empty strings, if that is what you want. In Java, split does not return the matched groups in addition to the splits (as it works in some other languages). Commented Aug 19, 2013 at 10:35
  • 2
    The regex [\\S\\s] is insane - it means "any character that is a whitespace or not a whitespace. It's the same as . Commented Aug 19, 2013 at 10:53
  • Thanks for the input Bohemian.. I had started with . and forgot to put it back. Commented Aug 19, 2013 at 11:50
  • @Bohemian only in combination with the dotall modifier. Commented Aug 19, 2013 at 12:14
  • @m.buettner fair enough Commented Aug 19, 2013 at 12:21

5 Answers 5

2

You regex matches the whole string. Thus, when splitting, the whole string gets removed. It is exactly the same as "a".split("a"), which returns an empty array.

What you could use instead is:

queryText.replaceAll(".*name <equals> `([^`]+)`.*", "$1")

which returns some name.

Sign up to request clarification or add additional context in comments.

Comments

0

Your regex expression covers all the string. Try to remove "^" and "$". Split method wants the expressions to remove not the expressions that must be found to detect a particular substring.

Comments

0

Try using the regex

((?<=`)[^`]*(?=`$))

as this gives you the result "some name" from

id <equals> `1` <AND> name <equals> `some name`

or if the "some name" does not Always appears at the end you can use

((?<=name <equals> `)[^`]*(?=`))

with the same result

Comments

0

Thanks for all of the input. [m.buettner] was correct. I was merely misunderstanding what String.split() is supposed to do. Per the Java documentation..

This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

This isn't what I wanted. I will merely create a util method with my Pattern + Matcher.

Thanks again everyone.

Comments

-1

Your regex variable is not properly formed for the String split() method... Try breaking the regex as shown in the answer of this thread...

java - split string using regular expression

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.