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 9285799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:15:27+00:00 2026-06-18T19:15:27+00:00

I am working on a program that gives the user the opportunity to define

  • 0

I am working on a program that gives the user the opportunity to define his own selection string. For example in the expression:

doc.select("a[href]");

The user would have specified the “a[href]” part. Now my question is, how can I check if the passed in string is a valid JSoup selector string? Does anyone know how to validate this?

Any help would be very much appreciated.

Thanks

  • 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-18T19:15:29+00:00Added an answer on June 18, 2026 at 7:15 pm

    approach 1: Edit the Jsoup source and make the Parser public or implement your own method there

    approach 2: Parse a simple dummy element and catch the exceptions. if one is thrown: query is not valid, else it’s ok. Not the best solution but it works.

    Here’s an example:

    private static final Element dummy = new Element(Tag.valueOf("p"), ""); // used for "testparsing"
    
    // ...
    
    public static boolean isValid(String query)
    {
        if( query == null ) // Check for null
            return false;
    
        try
        {
            Selector.select(query, dummy); // Use the querystring on the dummy - returnvalue is not relevant
        }
        catch( Selector.SelectorParseException | IllegalArgumentException ex ) // these exceptions are thrown if something is not ok
        {
            return false; // If something is not ok, the query is invalid
        }
    
        return true; // All ok, query is valid
    }
    

    Test:

    System.out.println(isValid(null)); // not valid
    System.out.println(isValid("div.abc")); // valid
    System.out.println(isValid("p[")); // not valid
    System.out.println(isValid("a:matchesxy")); // not valid
    System.out.println(isValid("div > a")); // valid
    

    Testresult:

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

Sidebar

Related Questions

I'm working on a program that reads in users input as a string, which
I am working on a kiosk application which gives the user a selection of
I'm working on a program that will sort files based on extension I currently
I'm working on a program that asks for input, then calculates and posts the
I am working on a program that uses IE to display its help pages.
I'm working on a program that will need to delete a folder (and then
I'm working on a program that creates thumbnails of JPEG images on the fly.
I'm working on a program that accesses StarTeam using the StarTeam SDK for .NET.
I am working on a program that extracts real time quote for 900+ stocks
I'm working on a program that takes in a Wavefront .obj file and eventually

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.