0

I am trying to validate a RegEx string in Java but i cant get it to work properly. I am attempting to make sure the the following string "AB10XY" is always contained with the textField when performing a search.

I have the following line of code which ensures that AB and XY are in the textField but not the number:

boolean checkChar = ((textField.getText().contains("AB")) && (textField.getText().contains("XY")));

I would prefer to have something like :

boolean checkChar = ((textField.getText().contains("AB[\\d]{2}XY")));
4

4 回答 4

1

你可以试试这个方法

boolean checkChar = ((textField.getText().matches(".*?AB[\\d]{2}XY.*")));
于 2013-05-15T11:52:24.293 回答
1

我猜这个数字并不总是 10,所以你应该使用这样的东西:

boolean checkChar = textField.getText().matches(".*AB[\\d]{2}XY.*");
于 2013-05-15T11:53:03.393 回答
0

包含方法不支持正则表达式..您可以使用匹配方法

 textField.getText().matches(".*AB\\d{2}XY.*");
于 2013-05-15T11:52:33.003 回答
0

使用包java.util.regex处理 RegEx

boolean checkChar = java.util.regex.Pattern.compile("AB[\\d]{2}XY")
        .matcher(textField.getText()).find();
于 2013-05-15T11:54:13.207 回答