Q:

Separate search keywords and facets

I have query strings something along those lines:
String query = 'metabolism and heart and TAXONOMY:40674 AND curation:"Non curated" GO:GO:34212 and cancer'

I am trying to extract from this string query the searching keywords. The expected result for this query is
metabolism and heart and cancer

How I have been doing?
String regex = '[a-z_A-Z0-9]*:\"?.+\"?'
query.replaceAll(regex, '')

This snippet works well if the query string is put at the beginning of the query string like below:
String query = 'metabolism and heart and cancer and TAXONOMY:40674 AND curation:"Non curated" GO:GO:34212'

The facets are couples like curation:"Non curated" (i.e., having spaces in the value and wrapped by the double quotes) or GO:GO:34212 (i.e., having the second colon). Otherwise, they are patterns like TAXONOMY:234334.
What I am doing wrong here? Any help?

A:

You need to modify your regex to cover the alphabetical strings to also account for spaces and re-write your regex as this,
[a-z_A-Z0-9]*:(?:"\w+(?: \w+)"\s*|\w+(?: \w+)\s*)?

And replace the matched parts with empty string.
The problem with your regex is, this part in your regex \"?.+\"? has optional doublequotes due to which .+ matches greedily everything till the end of string and removes the rest of the string keeping only string before TAXONOMY: which is not what you desire.
For correctly addressing the problem you should try matching doublequoted string separately with non-doublequoted string. Also, since your string has space separated words, you need to use \w+(?: \w+) part for correctly matching space separated words to match. \s* after that consumes extra whitespaces that are not desired and finally matches all your string and replacement with empty string leaves you with the desired string.
Regex Demo
Try this Java code,
String s = "metabolism and heart and TAXONOMY:40674 AND curation:\"Non curated\" GO:GO:34212 and cancer";
System.out.println(s.replaceAll("[a-z_A-Z0-9]*:(?:\"\\w+(?: \\w+)\"\\s*|\\w+(?: \\w+)\\s*)?", ""));

Prints,
metabolism and heart and cancer

