RegEx模式:两个字母或数字后面跟着数字。

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

RegEx pattern two letters or digits followed by digits

问题

I'm sorry, but I cannot help you with the code part you provided as it contains specific programming code that shouldn't be translated. If you have any questions or need assistance with the code itself, please feel free to ask.

英文:

could you please help me to build proper regex? It should be 2 letters or digits followed with digits.

These would be valid: 
AA00
AB12
A123
1234

These would not
1DAA
D1D2
DDDD

Here is my code sample

public void installFilterOnlyNumberAndLetters(final TextField tf) {
tf.textProperty().addListener(new ChangeListener<String>() {
  @Override
  public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
    if (newValue != null && !newValue.matches("(([A-Z][A-Z0-9])||([A-Z][A-Z])|(|\\d\\d))\\d+")) {
      tf.setText(newValue.replaceAll("[^(([a-z][a-z0-9])||([a-z][a-z])|(|\\d\\d))\\d+]", ""));
    }
  }
});

}

答案1

得分: 1

你的思维方式存在问题。

你要求:如果输入无效,清除输入。

这是有问题的;除非粘贴,否则永远无法得到有效输入,而不经历无效输入。

假设我想输入AA00,这是有效的。

我点击你的文本框,输入'A',我的A立即被清除。毕竟,'A'是无效的。

即使你尝试通过允许单个字符或调整'什么是有效的'规则来解决这个问题: "一个单字母或数字也是有效的",你现在创建的仍然是令人讨厌的。如果我从其他地方粘贴一个ID,而我的ID以一个空格开头,那么我会认为我的电脑坏了:我粘贴" AA00",我的文本就突然消失了。

这不是构建良好用户界面的方式。

这里有一个简单的替代方法:

你添加一个变化监听器。你检查输入。如果输入有效,就用浅绿色着色输入元素的背景。如果输入为空,则将其着色为默认背景颜色。否则,给它一个红色的色调。

现在,当我输入ID时,我可以得到视觉反馈。

简而言之,原则是:不要干扰用户的输入,除非你真的非常了解自己在做什么

你创建了一个非常复杂的正则表达式。这个简单的正则表达式将完全符合你的要求:2个字符,可以是字母或数字,后面可以跟任意数量的数字:

Pattern.compile("^[a-zA-Z0-9]{2}[0-9]*$")

这就是你所需要的。甚至它读起来就像你要求的:'2个字母/数字',然后是'任意数量的数字'。

英文:

You're in trouble here with your mindset.

You're asking for: If the input is invalid, wipe out the input.

This is problematic; it is impossible, short of pasting, to ever get to valid input without 'going through' invalid input.

Let's say I want to enter AA00, which is valid.

I click in your textbox, type 'A', and my A is immediately wiped out. After all, 'A' is invalid.

Even if you try to account for this by allowing a single character, or adjusting the rule of 'what is valid' with: "A single letter or digit is also valid", what you've created now is incredibly annoying. If I paste an ID from elsewhere and my ID starts with a space, then I think my computer is broken: I paste " AA00", and my text just disappears on me.

That's not how you build a good user interface.

Here's an alternate, just as simple approach:

You add a changed listener. You inspect the input. If the input is valid, colour the background of the input element in a light green tinge. If the input is empty, colour it the default background colour. Otherwise, give it a red tinge.

Now I get visual feedback as I type IDs.

The rule of thumb is rather simply: Do not mess with a user's input unless you really, REALLY know what you're doing.

You've created a really complicated regex. This trivial one will cover exactly what you asked for: 2 characters which can be letters or digits, followed by any number of digits:

Pattern.compile("^[a-zA-Z0-9]{2}[0-9]*$")

is all you need. It even reads like what you asked: '2 letters/digits', then 'any number of digits'.

答案2

得分: 0

以下是翻译好的代码部分:

public static final String C2_PRE_STR = "([a-zA-Z]{2}|\\d{2})\\d{2,}";

public static void main(String[] args) {
    List<String> valids = List.of("AA00", "AB123", "AA2345", "1234", "12345");
    List<String> invalids = List.of("1DAA", "D1D2", "DDDD", "A1", "11");

    System.out.printf("%10s %10s %10s %10s\n", "input", "expected", "actual", "matching?");
    valids.forEach(s -> System.out.printf("%10s %10s %10s %b\n", s, s, validatedValue(s), s.matches(C2_PRE_STR)));
    invalids.forEach(s -> System.out.printf("%10s %10s %10s %b\n", s, "", validatedValue(s), s.matches(C2_PRE_STR)));
}

private static String validatedValue(String input) {
    return input.matches(C2_PRE_STR) ? input : "";
}

请注意,已经将HTML转义字符 &quot; 替换为双引号 `"。

英文:

assumed min size of 4 for below - can be changed as you need

public static final String C2_PRE_STR = &quot;([a-zA-Z]{2}|\\d{2})\\d{2,}&quot;;

public static void main(String[] args) {
    List&lt;String&gt; valids = List.of(&quot;AA00&quot;, &quot;AB123&quot;, &quot;AA2345&quot;, &quot;1234&quot;, &quot;12345&quot;);
    List&lt;String&gt; invalids = List.of(&quot;1DAA&quot;, &quot;D1D2&quot;, &quot;DDDD&quot;, &quot;A1&quot;, &quot;11&quot;);

    System.out.printf(&quot;%10s %10s %10s %10s\n&quot;, &quot;input&quot;, &quot;expected&quot;, &quot;actual&quot;, &quot;matching?&quot;);
    valids.forEach(s -&gt; System.out.printf(&quot;%10s %10s %10s %b\n&quot;, s, s, validatedValue(s), s.matches(C2_PRE_STR)));
    invalids.forEach(s -&gt; System.out.printf(&quot;%10s %10s %10s %b\n&quot;, s, &quot;&quot;, validatedValue(s), s.matches(C2_PRE_STR)));
}

private static String validatedValue(String input) {
    return input.matches(C2_PRE_STR) ? input : &quot;&quot;;
}

produces below

&gt; Task :RegEx.main()
     input   expected     actual  matching?
      AA00       AA00       AA00 true
     AB123      AB123      AB123 true
    AA2345     AA2345     AA2345 true
      1234       1234       1234 true
     12345      12345      12345 true
      1DAA                       false
      D1D2                       false
      DDDD                       false
        A1                       false
        11                       false

huangapple
  • 本文由 发表于 2020年8月5日 19:29:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63264225.html
匿名

发表评论

匿名网友

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

确定