You simply need to escape the special characters. Try:

[a-zA-Z0-9\-#\.\(\)\/%&\s]{0,19}

You can test your regular expressions on http://rubular.com/

Answer from Andy Triggs on Stack Overflow
🌐
How to do in Java
howtodoinjava.com › home › java regular expressions › regex for alphanumeric characters (+ example)
Regex for Alphanumeric Characters (+ Example)
June 29, 2024 - To create a regular expression that allows only alphanumeric characters, we can use the regex pattern ^[a-zA-Z0-9]+$. This pattern ensures that the string consists solely of uppercase alphabets, lowercase alphabets, and digits. Alphanumeric characters are all alphabets and numbers i.e.
🌐
Mkyong
mkyong.com › home › regular expressions › java regex check alphanumeric string
Java regex check alphanumeric string | mkyong.com
November 8, 2020 - The regex \w is equivalent to [A-Za-z0-9_], matches alphanumeric characters and underscore. ... package com.mkyong.regex.string; public class StringUnderscore { public static void main(String[] args) { String str = "Hello_World_Java"; // or ^[\\w]+$ if (str.matches("^[a-zA-Z0-9_]+$")) { ...
🌐
Java2Blog
java2blog.com › home › core java › validation › java regex for alphanumeric characters
Java Regex for alphanumeric characters - Java2Blog
January 11, 2021 - Here is the explaination of above regex. ^ : start of string [ : beginning of character group a-z : any lowercase letter A-Z : any uppercase letter 0-9 : any digit _ : underscore ]: end of character group * : zero or more of the given characters $ : end of string · If you do not want to allow empty string, you can use + instead of *. ... Only Alphanumeric in Java2blog ...
🌐
CodingTechRoom
codingtechroom.com › question › java-regex-match-alphanumeric-special-symbols
How to Use Java Regex to Match alphanumeric Characters and Special Symbols - CodingTechRoom
If you want to create a regex pattern that matches lowercase letters (a-z), uppercase letters (A-Z), digits (0-9), and specific special symbols such as period (.), underscore (_), and hyphen (-), you can follow this guideline. ... String regex = "[a-zA-Z0-9._-]+"; String input = "Example_1...
🌐
TutorialsPoint
tutorialspoint.com › java-regex-program-to-verify-whether-a-string-contains-at-least-one-alphanumeric-character
Java regex program to verify whether a String contains at least one alphanumeric character.
January 10, 2020 - Following regular expression matches a string that contains at least one alphanumeric characters − · "^.*[a-zA-Z0-9]+.*$"; Where, ^.* Matches the string starting with zero or more (any) characters. [a-zA-Z0-9]+ Matches at least one alpha-numeric character.
🌐
Coderanch
coderanch.com › t › 608867 › java › Regex-alpha-numeric-apostrophes-hyphens
Regex for alpha numeric ,apostrophes and hyphens with certian conditions (Java in General forum at Coderanch)
April 5, 2013 - Henry Wong wrote: Perhaps the Oracle tutorial on regular expressions is a good place to start here ... http://docs.oracle.com/javase/tutorial/essential/regex/ Assuming that the OP resolves the ambiguity by adding 'space' to the allowable set of characters then to use a single regex will likely require the use of 'zero-width negative lookahead' which is normally considered an advanced regex topic.
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › how-to-check-string-is-alphanumeric-or-not-using-regular-expression
How to check string is alphanumeric or not using Regular Expression - GeeksforGeeks
July 12, 2025 - Therefore, it is not an alphanumeric string. Approach: This problem can be solved by using Regular Expression. Get the string. Create a regular expression to check string is alphanumeric or not as mentioned below: regex = "^(?=.*[a-zA-Z])(?=.*[0-9])[A-Za-z0-9]+$";
Find elsewhere
🌐
UiPath Community
forum.uipath.com › help › studio
Regex Alphanumeric and Certain Special Characters - Studio - UiPath Community Forum
August 7, 2023 - Hello fellow RPA Developers, I would like to ask for help for Regex of Alphanumeric and Certain Special Characters. Input: Washington St., (Poblacion 3), St@a. Rita City, “New-York”, U/SA Expected Output: Washington St., Poblacion 3, Sta. Rita City, New York, USA As you can see on the output, ...
Top answer
1 of 3
2

You can use the below regex to achieve your purpose:

^[\w][\S]{0,8}$

Explanation of the above regex:

^ - Represents the start of the line.

[\w] - Matches a character from [0-9a-zA-Z_]. If you do not want _(underscore) then provide the character class manually.[0-9A-Za-z]

[\S]{0,8} - Matches any non-space character 0 to 8 times.

$ - Represents end of the line.

You can find the demo of the above regex here.

Implementation in java:(You can modify the code accordingly to suit your requirements)

import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main
{
    private static final Pattern pattern = Pattern.compile("^[\\w][\\S]{0,8}$", Pattern.MULTILINE);
    public static void main(String[] args) {
        final String string = "Hello!@+\n"
     + "t123123\n"
     + "H123!#\n"
     + "@adadsa\n"
     + "@3asdasd\n"
     + "%sdf\n"
     + "Helloworld1";
     
     Matcher matcher = pattern.matcher(string);
     while(matcher.find())
        System.out.println(matcher.group(0));
    }
}

You can find the sample run of the above implementation in here.

2 of 3
0

I think this should work given your description:

^[a-zA-Z0-9]\S{0,8}$

It's not clear to me that you want to put ^ at the front and $ at the end. That will mean that the entire line must match. If you just want the entire string to match, adding these won't change anything and your pattern won't be useful for searching.

If you haven't looked, the Pattern class javadocs have a lot of helpful info including supported character classes. Those are the Java 7 docs but I doubt these have changed much.

🌐
IT Explore
itexplore.org › homepage › tips › validating strings as alphanumeric using regex in java
Validating Strings as Alphanumeric Using Regex in Java | IT Explore
April 30, 2025 - To validate alphanumeric characters using regular expressions, use ^[a-zA-Z0-9]+$. This pattern matches strings composed of one or more alphanumeric characters, such as "3DModel".
Top answer
1 of 2
1

You could match either == or a question mark in a capturing group, and use a backreference to group 1 using \1

You can use the character class [a-zA-Z0-9] or extend it to use \w (Note to use A-Z instead of A-z)

(==|\?)\h*\w+(?:\h+\w+)*\h*\1
  • (==|\?) Capture group 1, match either == or ?
  • \h* Match 1+ horizontal whitespace chars
  • \w+ Match 1+ word chars
  • (?:\h+\w+)* Optionally repeat matching the horizontal whitespace chars and word chars
  • \h* Match 1+ horizontal whitespace chars
  • \1 Backreference to group 1

Regex demo

In Java

String regex = "(==|\\?)\\h*\\w+(?:\\h+\\w+)*\\h*\\1";
2 of 2
1

This regex (== [A-Za-z0-9 _,.?!"'\-]+ ==)|(\?[A-Za-z0-9 _,.?!"'\-]+\?) matches alphanumeric characters and punctuation between 2 equal signs (and a space too) or 2 question marks (without spaces). You may add other characters between the square brackets if you wish (for example, ">").

[A-Za-z0-9 _,.?!"'\-] matches letters, numbers, underscores, commas, periods, question marks, exclamation marks, double quotes, single quotes, and hyphens.

Link to online regex tester: https://regex101.com/r/aqUtfn/2

EDIT: Another way to do it (I changed the answer by 'the fourth bird' around a bit to make it more strict) - (==|\?)((?<!\?)\s)?[[A-Za-z:;?!,."'][A-Za-z :;?!,."']+?\2?\1.

This will still match the stuff above (2 equal signs, a space, alphanumeric characters and punctuation, a space, and 2 more equal signs or a question mark, alphanumeric characters and punctuation, and another question mark), but uses backreferences to do it.

Link to online regex tester: https://regex101.com/r/aqUtfn/4

🌐
Coderanch
coderanch.com › t › 608393 › java › Java-regex-find-atleast-occurrence
Java regex to find atleast one occurrence of alphanumeric characters (Beginning Java forum at Coderanch)
March 29, 2013 - Aditya Sirohi wrote: I am using following regex pattern --> "^[a-zA-Z0-9]+$" But it not seem seem to return false when any one of the character is missing from the above catogories. Your regex doesn't say anything about the individual catagories.