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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:52:50+00:00 2026-06-13T16:52:50+00:00

I have a String, where only numbers and none, one or more percentages are

  • 0

I have a String, where only numbers and none, one or more percentages are allowed

so my regex would be: [\d+%], you can test it here

for java i have to transform it,

public static final String regex = "[\\d+\\%]";

and to test it i use this function

public static final String regex = "[\\d+\\%]";

public boolean validate(String myString){

    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(myString);

    if (!matcher.matches()) {
        return false;
    }else{
        return true;
    }
}

The regular expression is not working, also if i use

public static final String regex = "[\\d+%]";

Is there any good online tool for escaping a long regular expression for java?

A more advanced question:
the % should be only allowed if a minimum of one digit is in the String, only a % shouldn’t be allowed! And: numbers without a % are only allowed if the number of digits is exactly 8, not less (means: 1234567 is bad, but 12345678 is good)

Testcases:

  • Bad: %, (empty string), 23b, -1, 7.5, %5a, 1, 1234567
  • Good: 12345678, 23%, 1%53%53, %7
  • 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-13T16:52:51+00:00Added an answer on June 13, 2026 at 4:52 pm

    I have a String, where only numbers and none, one or more percentages are allowed so my regex would be: [\d+%]

    Actually, that matches ONE character which may be a digit, a + or a %.

    To match what you have described in words, you need something like this:

      [\d%]*\d[\d%]*
    

    which matches a string containing at least one digit with optional percent signs. Note that the % character is not a meta-character and hence doesn’t need to be escaped in the regex. It will match all of the following:

      0
      00
      %0
      0%0
      00%
      0%0%0
      0%%0
    

    and so on, but not just % or any string that contains characters other than digits or % characterss.


    Is there any good online tool for escaping a long regular expression for java?

    I’m not aware of one. But escaping wasn’t the reason your regex wasn’t working.


    A more advanced question: the % should be only allowed if a minimum of one digit is in the String, only a % shouldn’t be allowed!

    I think my regex above does that. And for the record, here is what it looks like as a Java String literal:

    "[\\d%]*\\d[\\d%]*"
    

    Unless you have TAB, NL, CR, etc characters in the regex , it is sufficient to just replace each individual \ with \\.

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

Sidebar

Related Questions

I have print $str; abcd*%1234$sdfsd..#d The string would always have only one continuous stretch
I have a string which contains only numbers. Now I want to remove all
I have a requirement that a user is allowed only to enter string value
I have a several processes node.js. I want to only add one string to
I have a string and need a RegEx Pattern for this, so I can
I have number of line breaks in a string. But I only want it
goal: I have the string 1234432144 I want to only replace the first 2
i want a regular expression to validate string to have only text,operators and these
For example, I have this string that could change anytime I only want the
I have simple form like this which accepts only two values string action and

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.