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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:11:05+00:00 2026-06-06T00:11:05+00:00

I have a problem with regards to writing the correct regex for the examination

  • 0

I have a problem with regards to writing the correct regex for the examination rating field of my grails application. Users should have the option to either enter an alphabet character OR a number. Quantitative rating should range from 0-100, accepts decimal numbers with or without percentage sign (%) but exclude negative numbers. The regex should permit special characters percentage(%) and period (.) ONLY. Below is a set of possible input:

1) 100% (should pass the validation)
2) 100.0 (should pass the validation)
3) PASSED (should pass the validation)
4) FAILED (should pass the validation)
5) 100.1 (should not pass the validation)
6) -90 (should not pass the validation)

Your answer will be highly 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-06T00:11:07+00:00Added an answer on June 6, 2026 at 12:11 am

    I assume the language is Java. I also assume that you want to validate user input, which expects to contain only the percentage.

    "^\\s*((100(\\.0*)?|\\d{1,2}(\\.\\d*)?|\\.\\d+)%?|PASSED|FAILED)\\s*$"
    

    (In Groovy, you don’t have to escape the backslash)

    /^\s*((100(\.0*)?|\d{1,2}(\.\d*)?|\.\d+)%?|PASSED|FAILED)\s*$/ (for Groovy)
    

    The regex above will only validate numbers between 0.0 to 100.0. It allows arbitrary long number of digits after decimal points, and also allows cases such as .8 or 45. to pass, since they can be processed. The regex also allows any amount of spaces before and after the input (but not inside).

    EDIT

    To match plain text (ASCII):

    "^\\s*((100(\\.0*)?|\\d{1,2}(\\.\\d*)?|\\.\\d+)%?|[a-zA-Z.]+)\\s*$"
    
    /^\s*((100(\.0*)?|\d{1,2}(\.\d*)?|\.\d+)%?|[a-zA-Z.]+)\s*$/ (for Groovy)
    

    This will match text that uses English alphabet character (case-insensitive), and full-period. E.g. sdhSDFHS.sfSDJF.sdfSDJFI will matches the above regex. Note that the current regex does not allow spaces within the text, but if you want to add more characters, add inside the square bracket (character class): [a-zA-Z.]. Check the documentation for more details.

    Testing code (for Java)

    String arr[] = {"100%", "  .435%", "3%", "0%", "0", ".0", "hdfjs.", "  PASSED", "FAILED", "34.5  ", "sdhf.sdfdsf.fsdf  ", " 100.0000 ", "-234.4", "-0.1", "34.", "100.0000001", "1000"};
    for (String s: arr) 
        System.out.println(s + " " + (s.matches("^\\s*((100(\\.0*)?|\\d{1,2}(\\.\\d*)?|\\.\\d+)%?|[a-zA-Z.]+)\\s*$") ? "matched" : "not matched"));
    

    Output

    100% matched
      .435% matched
    3% matched
    0% matched
    0 matched
    .0 matched
    hdfjs. matched
      PASSED matched
    FAILED matched
    34.5   matched
    sdhf.sdfdsf.fsdf   matched
     100.0000  matched
    -234.4 not matched
    -0.1 not matched
    34. matched
    100.0000001 not matched
    1000 not matched
    

    EDIT2

    Change in requirement as mentioned in the discussion: only match from 1.0 to 100.0 inclusive for the percentage:

    /^\s*((100(\.0*)?|[1-9]\d?(\.\d*)?)%?|[a-zA-Z.]+)\s*$/ (for Groovy)
    

    This condition is even more stricter than before, since 01.1 will not match. If you want to match this case:

    /^\s*(0*((100(\.0*)?|[1-9]\d?(\.\d*)?)%?)|[a-zA-Z.]+)\s*$/ (for Groovy)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I writing a wrapper in c# and i have some problem. I have
I'm writing a JSF 2.0 application with Hibernate and Postgresql database. My problem is
I am writing a distributed application in Scala that uses Akka actors. I have
I have a problem. I'm writing an app in Polish (with, of course, polish
I have this problem in regards with default, since I m not sure whether
I am having a problem in writing the query for this. I have a
I have an application which uses some external library for analytics. Problem is that
I have problem creating new instance of excel 2007 using VBA (from Access 2002).
I have problem with fancybox. I want to write a function that will run
i have problem with autorotate on iphone i set up in all classes -

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.