英文:
Invalid regular expression : Dangling meta character "*"
问题
我正在为JavaFX创建一个带有登录和用户名的身份识别窗口,并且我正在使用正则表达式确保密码至少包含一个特殊字符和一个数字。
这是我正在使用的正则表达式
newValue.matches(".*\\d+.*\\W+.*")
它在在线测试网站上起作用,但是当我在文本字段中输入任何内容时,我会遇到错误。以下是我正在使用的代码片段:
public void progressBarHandler() {
login.textProperty().addListener((observable, oldValue, newValue) -> {
if(newValue.matches(".*\\d+.*\\W+.*")) {
passState.textProperty().set("Bad");
}
else passState.textProperty().set("Ok");
});
}
每当我运行代码并在登录文本字段中写入内容时,我会收到以下错误:
Exception in thread "JavaFX Application Thread" java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0
英文:
I am creating an identification window with login and username for JavaFX, and I am using regex to make sure that the password contains at least one special character and one digit.
This is the regular expression I'm using
newValue.matches("*\\d+.*\\W+.*")
It works on testing sites online however I am getting and error while I enter any input in the textfield. Here is the segment of code I am using:
public void progressBarHandler() {
login.textProperty().addListener((observable, oldValue, newValue) -> {
if(newValue.matches("*\\d+.*\\W+.*")) {
passState.textProperty().set("Bad");
}
else passState.textProperty().set("Ok");
});
}
Whenever I run the code and I write something in the textfield login I get this error:
Exception in thread "JavaFX Application Thread" java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0
答案1
得分: 7
`*` 量词“重复”(量化)它所修改的模式零次或多次,以贪婪方式进行(允许正则表达式引擎“获取”(消耗)尽可能多的字符与给定模式匹配)。如果 `*` 或任何其他量词出现在模式的开头,那么量词无法修改任何模式,错误将会出现。
也就是说,将 `*abc*` 用作正则表达式的 glob 模式将产生相同的问题,以及 `{1,}abc.*`、`+abc.*` 或 `{5}+abc.*` 等。
你可以通过在这里在 `*` 前面简单地添加一个点来修复这个模式,因为你希望匹配包含 “数字……非单词字符……” 模式的字符串:
```java
newValue.matches(".*\\d+.*\\W+.*")
// ^
然而,一个更好、更高效的模式在这里是:
newValue.matches("\\D*\\d\\w*\\W.*")
它匹配
- 字符串的开头(
matches
需要完全的字符串匹配) \D*
- 零个或多个非数字字符\d
- 一个数字\w*
- 0 个或多个单词字符\W
- 一个非单词字符.*
- 除了换行符之外的任意零个或多个字符,尽可能多地匹配。- 字符串的结尾(
matches
需要完全的字符串匹配)。
<details>
<summary>英文:</summary>
The `*` quantifier "repeats" (quantifies) the pattern it modifies zero or more times in a greedy way (allows the regex engine to "take" (=consume) as many chars as it can with the given pattern). If `*` or any other quantifier appears at the start of a pattern, there is no pattern the quantifier can modify, and the error appears.
That is, `*abc*` glob pattern used as a regex will produce this same issue, as well as `{1,}abc.*`, `+abc.*` or `{5}+abc.*`, etc.
You may fix the pattern by simply adding a dot in front of the `*` here since you expect to match a string that contains a pattern `digits...non-word chars...`:
```java
newValue.matches(".*\\d+.*\\W+.*")
// ^
However, a better, more efficient pattern here would be
newValue.matches("\\D*\\d\\w*\\W.*")
It matches
- start of string (
matches
requires a full string match) \D*
- zero or more non-digit chars\d
- a digit\w*
- 0 or more word chars\W
- a non-word char.*
- any zero or more chars other than line break chars as many as possible.- end of string (
matches
requires a full string match).
答案2
得分: 3
在正则表达式中,Kleene star(克里尼星号)*
表示“前一个字符(字符类)或群组的零个或多个匹配”。但在您的情况下,前面没有任何内容,星号是您的正则表达式字符串中的第一个字符。这就是错误指示的内容。
如果您实际上想要匹配一个星号,您必须使用反斜杠进行转义:
"\\*\\d+.*\\W+.*"
英文:
In regular expressions, the Kleene star *
means "zero or more matches of the preceding character (class) or group". But in your case, there is nothing preceding, the star is the first character in your regex string. That's what the error indicates.
If you actually want to match an asterisk you have to escape it with backslashs:
"\\*\\d+.*\\W+.*"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论