简体   繁体   English

我需要一个Java正则表达式

[英]I need a Java regular expression

I am currently using the following regular expression: 我目前正在使用以下正则表达式:

^[a-zA-Z]{0,}(\\*?)?[a-zA-Z0-9]{0,}

to check a string to start with an alpha character and end with alphanumeric characters and have an asterisk(*) anywhere in the string but only a maximum of one time. 检查字符串以字母字符开头,以字母数字字符结尾,并在字符串中的任何地方(但最多只能有一次)都带有星号(*)。 The problem here is that if the given string still passes if it starts with a number but doesn't have an *, which should fail. 这里的问题是,如果给定的字符串以数字开头但没有*,该字符串仍然通过,则该字符串将失败。 How can I rework the regex to fail this case? 我该如何重做正则表达式以解决这种情况?

ex. 恩。

TE - pass

*TE - pass

TE* - pass

T*E - pass

*9TE - pass

*TE* - fail (multiple asterisk)

9E - fail (starts with number)

EDIT: Sorry to introduce a late edit but I also need to ensure that the string is 8 characters or less, can I include that in the regex as well? 编辑:很抱歉,介绍较晚的编辑,但是我还需要确保该字符串为8个字符或更少,我也可以在正则表达式中包括它吗? Or should I just check the string length after the regex validation? 还是应该在正则表达式验证之后检查字符串长度?

This passes your example: 这通过了您的示例:

"^([a-zA-Z]+\\*?|\\*)[a-zA-Z0-9]*$"

It says:
  start with: [a-zA-Z]+\\*? (a letter and maybe a star)
              | (or)
              \\* a single star
  and end with [a-zA-Z0-9]* (an alphanumeric character)

Code to test it: 测试代码:

public static void main(final String[] args) {
    final Pattern p = Pattern.compile("^([a-zA-Z]+\\*?|\\*)\\w*$");

    System.out.println(p.matcher("TE").matches());
    System.out.println(p.matcher("*TE").matches());
    System.out.println(p.matcher("TE*").matches());
    System.out.println(p.matcher("T*E").matches());
    System.out.println(p.matcher("*9TE").matches());
    System.out.println(p.matcher("*TE*").matches());
    System.out.println(p.matcher("9E").matches());
}

Per Stargazer, if you allow alphanumeric before the star, then use this: 对于观星者,如果您允许在星号前使用字母数字,请使用以下命令:

^([a-zA-Z][a-zA-Z0-9]*\\*?|\\*)\\w*$

One possible way is to separate into 2 conditions: 一种可能的方法是分为两个条件:

^(?=[^*]*\*?[^*]*$)[a-zA-Z*][a-zA-Z0-9*]*$
  • The (?=[^*]*\\*?[^*]*$) part ensures there is at most one * in the string. (?=[^*]*\\*?[^*]*$)部分确保字符串中最多有一个*
  • The [a-zA-Z*][a-zA-Z0-9*]* part ensures it starts with an alphabet or a * , and followed by only alphanumerals or * . [a-zA-Z*][a-zA-Z0-9*]*部分确保它以字母或*开头,然后仅字母数字或*开头。

It might be easier to develop and maintain later if you just break your regular expressions into a few pieces, eg, one for the start and end, and one for the asterisk. 如果只将正则表达式分成几部分,例如,一个用于开始和结束,一个用于星号,则可能会更容易开发和以后维护。 I am not sure what the overall performance effect would be, you would have simpler expressions but have to run a few of them. 我不确定整体性能会如何,您将使用更简单的表达式,但必须运行其中的一些表达式。

寻找两种可能的模式,一种以*开头,另一种以字母char开头:

^[a-zA-Z][a-zA-Z0-9]*(\\*?)?[a-zA-Z0-9]*|\*[a-zA-Z0-9]*

How about this, it's easier to read: 怎么样,它更容易阅读:

boolean pass = input.replaceFirst("\\*", "").matches("^[a-zA-Z].*\\w$");

Assuming I read right, you want to: 假设我没看错,您想:

  1. Start with an alpha character 以字母字符开头
  2. End with an alphanumeric character 以字母数字字符结尾
  3. Allow up to one * anywhere 任何地方最多允许一个*

^([a-zA-Z][a-zA-Z0-9]*\\*|\\*|[a-zA-Z])([a-zA-Z0-9])*$

下半部的括号是为了清楚起见,可以安全地排除在外。

This was a tough one (liked the challenge), but here it is: 这是一项艰巨的任务(喜欢挑战),但这是:

^(\*[a-zA-Z0-9]+|[a-zA-Z]+[\*]{1}[a-zA-Z]*)$

In order to comply with T9*Z, as pointed out on another post with StarGazer712, I had to change it to: 为了遵守T9 * Z,正如在StarGazer712的另一篇文章中指出的那样,我不得不将其更改为:

^(\*[a-zA-Z0-9]+|[a-zA-Z]{1}[a-zA-Z0-9]*[\*]{1}[a-zA-Z0-9]*)$

This is Python, it'll need some massaging for Java: 这是Python,需要对Java进行一些按摩:


>>> import re
>>> p = re.compile('^([a-z][^*]*[*]?[^*]*[a-z0-9]|[*][^*]*[a-z0-9]|[a-z][^*]*[*])$', re.I)
>>> for test in ['TE', '*TE', 'TE*', 'T*E', '*9TE', '*TE*', '9E']:
...  if p.match(test):
...   print test, 'pass'
...  else:
...   print test, 'fail'
... 
TE pass
*TE pass
TE* pass
T*E pass
*9TE pass
*TE* fail
9E fail

Hope I didn't miss anything. 希望我什么都不会错过。

At most one asterisk, alphabetic characters anywhere and numbers anywhere but at start. 最多一个星号,任何字母字母字符都可以,除了开始处,数字都可以。

    String alpha = "[a-zA-Z]";
    String alnum = "[a-zA-Z0-9]";

    String asteriskNone = "^" + alpha + "+" + alnum + "*";
    String asteriskStart = "^\\*" + alnum + "*";
    String asteriskInside = "^" + alpha + "+" + alnum + "+\\*" + alnum + "*";
    String yourRegex = asteriskNone + "|" + asteriskStart + "|"
            + asteriskInside;
    String[] tests = {"TE","*TE","TE*","T*E","*9TE","*TE*", "9E"};
    for (String test : tests)
        System.out.println(test + " " + (test.matches(yourRegex)?"PASS":"FAIL"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM