Richa Khetan
Richa Khetan

Reputation: 37

Regex that starts with alphanumeric character and can have special characters in middle of the string(Java)

I want a regex to start with alphanumeric but can contain special character in the middle of the string. Length of the string should be less than 10 including first character.

Accepted Inputs:

Hello!@+
t123123
H123!#

Rejected Inputs:

@adadsa
@3asdasd
%sdf

I have tried

"^([a-zA-Z0-9]{1}).[a-zA-Z0-9@#$%^&+=]{2,10}$"

Upvotes: 0

Views: 1867

Answers (3)

user7571182
user7571182

Reputation:

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.

pictorial representation

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.

Upvotes: 2

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79055

Use [\\p{Alnum}][\\S]{0,8} as the regex.

Demo:

public class Main {
    public static void main(String[] args) {
        String[] arr = { "Hello!@+", "Hello!@+asdf", "t123123", "H123!#", "@adadsa", "@3asdasd", "%sdf" };
        String pattern = "[\\p{Alnum}][\\S]{0,8}";
        for (String s : arr) {
            System.out.println(s + " => " + (s.matches(pattern) ? "matches" : "does not match"));
        }
    }
}

Output:

Hello!@+ => matches
Hello!@+asdf => does not match
t123123 => matches
H123!# => matches
@adadsa => does not match
@3asdasd => does not match
%sdf => does not match

Explanation:

  1. \\p{Alnum} matches an alphanumeric character and \\S matches a non-whitespace character.
  2. Use [\\S]{0,8} to ensure that the next up to eight characters can be anything.

Upvotes: 0

JimmyJames
JimmyJames

Reputation: 1403

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.

Upvotes: 0

Related Questions