0

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}$"
  • 1
    You should also add what you have tried – Deepak Jun 05 '20 at 15:59
  • What's the rationale for the `{2,4}` in your expression? – khelwood Jun 05 '20 at 16:06
  • @khelwood I have updated it. i have inserted that to check length of the string – user8588354 Jun 05 '20 at 16:10
  • Does [this](https://regex101.com/r/dW5WxN/2) help? –  Jun 05 '20 at 17:29
  • 1
    @user8588354 - If one of the answers resolved your issue, you can help the community by marking it as accepted. An accepted answer helps future visitors use the solution confidently. Check https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work to learn how to do it. – Arvind Kumar Avinash Jun 07 '20 at 15:15

3 Answers3

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.

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.

Community
  • 1
  • 1
  • $ and ^ represent the beginning and end of a line, not a string, If you are using multi-line mode they may behave differently though. – JimmyJames Jun 05 '20 at 18:10
  • 1
    both instances of [] are unnecessary here. – JimmyJames Jun 05 '20 at 18:12
  • updated @JimmyJames. Thanks. Instances are fine btw; they improve readability. –  Jun 05 '20 at 18:14
  • 1
    "they improve readability" How so? They don't change anything and make it look like there's something more complex going on. There are also subtle differences to how things are normally interpreted and how they are interpreted within []. Basically your are adding "choose one from this list of one" for no reason. – JimmyJames Jun 05 '20 at 21:15
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.

JimmyJames
  • 1,114
  • 11
  • 20
  • @Mandy If you use the matches() function from pattern or Matcher, the entire string must match. – JimmyJames Jun 05 '20 at 18:08
  • Right @JimmyJames. But maybe he want to add multiple test case. But I agree with you on this. –  Jun 05 '20 at 18:12
0

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.
Arvind Kumar Avinash
  • 50,121
  • 5
  • 26
  • 72