以字母数字字符开头,并且字符串中间可以有特殊字符的正则表达式(Java)

huangapple go评论102阅读模式
英文:

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

问题

我想要一个正则表达式,以字母或数字开头,但字符串中间可以包含特殊字符。字符串的长度应小于10,包括第一个字符。

接受的输入:

Hello!@+
t123123
H123!#

拒绝的输入:

@adadsa
@3asdasd
%sdf

我尝试过:

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

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

得分: 2

你可以使用以下正则表达式实现你的目的:

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

对上述正则表达式的解释:
> ^ - 表示行的开头。
>
> [\w] - 匹配字符范围为 [0-9a-zA-Z_]。如果你不想包含下划线 _,可以手动提供字符范围 [0-9A-Za-z]
>
> [\S]{0,8} - 匹配任意非空格字符,出现 0 到 8 次。
>
> $ - 表示行的结尾。

你可以在这里找到上述正则表达式的演示:这里

在 Java 中的实现(你可以根据需要修改代码):

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 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.

以字母数字字符开头,并且字符串中间可以有特殊字符的正则表达式(Java)

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

得分: 0

我认为根据您的描述,这应该可以工作:

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

我不太清楚您是否希望在开头添加^并在结尾处添加$。这将意味着整行必须完全匹配。如果您只希望整个字符串匹配,添加这些字符将不会改变任何内容,您的模式对于搜索将不会有用。

如果您还没有查看过,Pattern 类的 Java 文档提供了许多有用的信息,包括支持的字符类。这些是 Java 7 的文档,但我认为这些内容没有太大变化。

英文:

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.

答案3

得分: 0

使用 [\\p{Alnum}][\\S]{0,8} 作为正则表达式。

演示:

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) ? "匹配" : "不匹配"));
        }
    }
}

输出:

Hello!@+ => 匹配
Hello!@+asdf => 不匹配
t123123 => 匹配
H123!# => 匹配
@adadsa => 不匹配
@3asdasd => 不匹配
%sdf => 不匹配

解释:

  1. \\p{Alnum} 匹配字母数字字符,\\S 匹配非空白字符。
  2. 使用 [\\S]{0,8} 确保接下来的最多八个字符可以是任何字符。
英文:

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.

huangapple
  • 本文由 发表于 2020年6月5日 23:58:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/62219457.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定