0
str = "sysparm_type=list_data&count=20&start=0&p=incident%3Bq%3Aactive%3Dtrue%5Epriority%3D1%5EEQ&table=incident"

I've written a regex for the above string but i want to match it only if the "priority" substring is not present. Here's my regex:

.*sysparm_type=list_data&count=(\d+)&start=(\d+)&p=incident.*active.*true^((?!priority).)*$&table=incident

But this part ^((?!priority).)*$ of the regex isn't working.

9
  • 1
    Remove ^ and $ from the tempered greedy token. Commented Oct 8, 2015 at 15:24
  • 2
    Aren't you better off mapping it as a GET parameter list to an actual request map, then checking if priority is present in the keys? Commented Oct 8, 2015 at 15:24
  • 1
    may I recommend regex101.com as it will allow you to dig into your regex Commented Oct 8, 2015 at 15:24
  • 2
    Regex is NOT the solution to the universe. Try to use the appropriate tools for your problem (like split). Commented Oct 8, 2015 at 15:24
  • 1
    Now you have two problems. Commented Oct 8, 2015 at 15:25

3 Answers 3

1

Caret ^ and $ match at the beginning and end of the entire string. Very greedy.

Wouldn't it be simpler, easier to read and maintain if you just checked for the profile string to be present?

    String pattern = "((?!priority).)*";  <== pasted from above, prob not valid regex

  // Create a Pattern object
  Pattern r = Pattern.compile(pattern);

  // Now create matcher object.
  Matcher m = r.matcher(line);
  if (m.find( )) {
    System.out.println("Found value" );

  } else {
     System.out.println("NO MATCH");
  }
Sign up to request clarification or add additional context in comments.

Comments

1

(a) regexp is bad solution for query string parsing - slow, memory consuming, error prone

(b) try to use any existing library for this task, e.g. apache commons:

String str = "sysparm_type=list_data&count=20&start=0&p=incident%3Bq%3Aactive%3Dtrue%5Epriority%3D1%5EEQ&table=incident";
List<NameValuePair> pairs = URLEncodedUtils.parse(str, StandardCharsets.UTF8)
for (NameValuePair pair : pairs)
    if (pair.getName().equals("priority"))
        return; // do nothing

3 Comments

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
@Tarec I've added example snippet
@ursa I understand your point, but in this case I'm restricted to use a regex only to match. Anyways stribizhev solution did the trick. Thanks all your suggestions.
0

If you want to check subString a simple solution will be checking str.indexOf(subString) will return a int if it's greater than 0 then str contains subString.

If you want to do it in regExp use the pattern .*priority.*, you can some more restriction of the substring if you're sure about its occurrences and position

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.