简体   繁体   English

RegEx 模式两个字母或数字后跟数字

[英]RegEx pattern two letters or digits followed by digits

could you please help me to build proper regex?你能帮我建立正确的正则表达式吗? It should be 2 letters or digits followed with digits.它应该是 2 个字母或数字后跟数字。

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+]", ""));
    }
  }
});

} }

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.假设我要输入有效的 AA00。

I click in your textbox, type 'A', and my A is immediately wiped out.我单击您的文本框,输入“A”,然后我的 A 立即消失。 After all, 'A' is invalid.毕竟,“A”是无效的。

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.如果我从其他地方粘贴一个 ID,而我的 ID 以空格开头,那么我认为我的电脑坏了:我粘贴“AA00”,我的文字就消失了。

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.现在,当我键入 ID 时,我会得到视觉反馈。

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:这个简单的将完全涵盖您要求的内容:2 个字符,可以是字母或数字,后跟任意数量的数字:

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 个字母/数字”,然后是“任意数量的数字”。

assumed min size of 4 for below - can be changed as you need假设以下最小大小为 4 - 可以根据需要更改

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 : "";
}

produces below在下面产生

> 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

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

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