Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8646847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:55:07+00:00 2026-06-12T12:55:07+00:00

I’m matching URLs against a regular expression, testing if they reflect a shutdown command.

  • 0

I’m matching URLs against a regular expression, testing if they reflect a “shutdown” command.

Here’s a URL that performs a shutdown:

/exec?debug=true&command=shutdown&f=0

Here’s another, legitimate but confusing URL that performs shutdown:

/exec?commando=yes&zcommand=34&command=shutdown&p

Now, I must ensure there’s only one command=… parameter and it is command=shutdown. Alternatively, I can live with ensuring the first command=… parameter is command=shutdown.

Here’s my test for the requested regular expression:

/exec?version=0.4&command=shutdown&out=JSON&zcommand=1

Should match

/exec?version=0.4&command=startup&out=JSON&zcommand=1&commando=shutdown

Should fail to match

/exec?command=shutdown&out=JSON

Should match

/exec?version=0.4&command=admin&out=JSON&zcommand=1&command=shutdown

Should fail to match

Here’s my baseline – a regular expression that passes the above tests – all but the last one:

^/exec?(.*\&)*command=shutdown(\&.*)*$

The problem is with the occurrence of more than one command=…, where the first one is not shutdown.

I tried using lookbehind:

^/exec?(.*\&)*(?<!(\&|\?)command=.*)command=shutdown(\&.*)*$

But I’m getting:

Look-behind group does not have an obvious maximum length near index 31

I even tried atomic grouping. To no avail. I can’t make the following expression NOT match:

/exec?version=0.4&command=admin&out=JSON&zcommand=1&command=shutdown

Can anyone help with a regular expression that passes all the tests?

Clarifications

I see I owe you some context.

My task is to configure a Filter that guards the entrance of all our system’s servlets, and verifies there’s an open HTTP session (in other words: that a successful Login has occurred). The filter also allows configuring which URLs do not require login.

Some exceptions are easy: /login does not need login. Calls to localhost do not need login.

But sometimes it gets complicated. Like the shutdown command that cannot require login while other commands can and should (the strange reason for that is out of the scope of my question).

Since it’s a security matter, I can’t allow users to merely append &command=shutdown to a URL and bypass the filter.

So I really need a regular expression, or otherwise I’ll need to redefine the configuration specs.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:55:09+00:00Added an answer on June 12, 2026 at 12:55 pm

    This tested (and fully commented) regex solution meets all your requirements:

    import java.util.regex.*;
    public class TEST {
        public static void main(String[] args) {
            Pattern re = Pattern.compile(
                "  # Match URI having command=shutdown query variable value. \n" +
                "  ^                          # Anchor to start of string.   \n" +
                "  (?:[^:/?\\#\\s]+:)?        # URI scheme (Optional).       \n" +
                "  (?://[^/?\\#\\s]*)?        # URI authority (Optional).    \n" +
                "  [^?\\#\\s]*                # URI path.                    \n" +
                "  \\?                        # Literal start of URI query.  \n" +
                "    # Match var=value pairs preceding 'command=xxx'.        \n" +
                "  (?:                        # Zero or more 'var=values'    \n" +
                "    (?!command=)             # only if not-'command=xxx'.   \n" +
                "    [^&\\#\\s]*              # Next var=value.              \n" +
                "    &                        # var=value separator.         \n" +
                "  )*                         # Zero or more 'var=values'    \n" +
                "  command=shutdown           # variable and value to match. \n" +
                "    # Match var=value pairs following 'command=shutdown'.   \n" +
                "  (?:                        # Zero or more 'var=values'    \n" +
                "    &                        # var=value separator.         \n" +
                "    (?!command=)             # only if not-'command=xxx'.   \n" +
                "    [^&\\#\\s]*              # Next var=value.              \n" +
                "  )*                         # Zero or more 'var=values'    \n" +
                "  (?:\\#\\S*)?               # URI fragment (Optional).     \n" +
                "  $                          # Anchor to end of string.", 
                Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.COMMENTS);
            String s = "/exec?version=0.4&command=shutdown&out=JSON&zcommand=1";
                // Should match
    //      String s = "/exec?version=0.4&command=startup&out=JSON&zcommand=1&commando=shutdown";
                // Should fail to match 
    //      String s = "/exec?command=shutdown&out=JSON";
                // Should match
    //      String s = "/exec?version=0.4&command=admin&out=JSON&zcommand=1&command=shutdown";
            // Should fail to match";
            Matcher m = re.matcher(s);
            if (m.find()) {
                // Successful match
                System.out.print("Match found.\n");
            } else {
                // Match attempt failed
                System.out.print("No match found.\n");
            } 
        }
    }
    

    The above regex matches any RFC3986 valid URI having any scheme, authority, path, query or fragment components, but it must have one (and only one) query "command" variable whose value must be exactly, but case insensitively: "shutdown".

    A carefully crafted complex regex is perfectly fine (and maintainable) to use when written with proper indentation and commented steps (like shown above). (For more information on using regex to validate a URI, see my article: Regular Expression URI Validation)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I know there's a lot of other questions out there that deal with this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.