为什么这个 Java 正则表达式不起作用?
我需要创建一个正则表达式,允许字符串包含任意数量的:
- 字母数字字符
- 空格
- (
- )
- &
- 。
不允许使用其他字符。 我使用 RegexBuddy 构建以下正则表达式,当我在 RegexBuddy 中测试它时它可以正常工作:
\w* *\(*\)*&*\.*
然后我使用 RegexBuddy 的“使用”功能将其转换为 Java 代码,但使用简单的测试程序似乎无法正常工作:
public class RegexTest
{
public static void main(String[] args)
{
String test = "(AT) & (T)."; // Should be valid
System.out.println("Test string matches: "
+ test.matches("\\w* *\\(*\\)*&*\\.*")); // Outputs false
}
}
- 我必须承认,在正则表达式方面我有一点盲点。 谁能解释一下为什么它不起作用?
I need to create a regular expression that allows a string to contain any number of:
- alphanumeric characters
- spaces
- (
- )
- &
- .
No other characters are permitted. I used RegexBuddy to construct the following regex, which works correctly when I test it within RegexBuddy:
\w* *\(*\)*&*\.*
Then I used RegexBuddy's "Use" feature to convert this into Java code, but it doesn't appear to work correctly using a simple test program:
public class RegexTest
{
public static void main(String[] args)
{
String test = "(AT) & (T)."; // Should be valid
System.out.println("Test string matches: "
+ test.matches("\\w* *\\(*\\)*&*\\.*")); // Outputs false
}
}
- I must admit that I have a bit of a blind spot when it comes to regular expressions. Can anyone explain why it doesn't work please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
该正则表达式测试是否有任意数量的空格,后跟任意数量的字母数字字符,后跟任意数量的左括号,后跟任意数量的右括号,后跟任意数量的 & 符号,后跟任意数量的句点。
你想要的是......
正如mmyers提到的,这允许空字符串。 如果您不想允许空字符串...
尽管这也将允许仅包含空格或仅句点等的字符串。如果您想确保至少有一个字母数字字符...
所以您明白正则表达式的含义...方括号(“[]”)内的任何内容都表示一组字符。 因此,其中“a*”表示 0 个或多个 a,[abc]* 表示 0 个或多个字符,所有这些字符都是 a、b 或 c。
That regular expression tests for any amount of whitespace, followed by any amount of alphanumeric characters, followed by any amount of open parens, followed by any amount of close parens, followed by any amount of ampersands, followed by any amount of periods.
What you want is...
As mentioned by mmyers, this allows the empty string. If you do not want to allow the empty string...
Though that will also allow a string that is only spaces, or only periods, etc.. If you want to ensure at least one alpha-numeric character...
So you understand what the regular expression is saying... anything within the square brackets ("[]") indicates a set of characters. So, where "a*" means 0 or more a's, [abc]* means 0 or more characters, all of which being a's, b's, or c's.
也许我误解了你的描述,但是你本质上不是定义了一类没有顺序而不是特定序列的字符吗? 您的正则表达式不应该具有 [xxxx]+ 结构,其中 xxxx 是您想要的实际字符吗?
Maybe I'm misunderstanding your description, but aren't you essentially defining a class of characters without an order rather than a specific sequence? Shouldn't your regexp have a structure of [xxxx]+, where xxxx are the actual characters you want ?
Java 代码片段和 RegexBuddy 中的“测试”选项卡之间的区别在于,Java 中的 matches() 方法需要正则表达式来匹配整个字符串,而 RegexBuddy 中的“测试”选项卡允许部分匹配。 如果您在 RegexBuddy 中使用原始正则表达式,您将看到多个黄色和蓝色突出显示的块。 这表明 RegexBuddy 在您的字符串中发现了多个部分匹配项。 要获得与 matches() 一起按预期工作的正则表达式,您需要对其进行编辑,直到整个测试主题以黄色突出显示,或者如果您关闭突出显示,直到“查找第一个”按钮选择整个文本。
或者,您可以在正则表达式的开头和结尾使用锚点 \A 和 \Z 来强制其匹配整个字符串。 当您这样做时,您的正则表达式始终以相同的方式运行,无论您在 RegexBuddy 中测试它,还是使用 matches() 或 Java 中的其他方法。 只有 matches() 需要完整的字符串匹配。 Java 中的所有其他 Matcher 方法都允许部分匹配。
The difference between your Java code snippet and the Test tab in RegexBuddy is that the matches() method in Java requires the regular expression to match the whole string, while the Test tab in RegexBuddy allows partial matches. If you use your original regex in RegexBuddy, you'll see multiple blocks of yellow and blue highlighting. That indicates RegexBuddy found multiple partial matches in your string. To get a regex that works as intended with matches(), you need to edit it until the whole test subject is highlighted in yellow, or if you turn off highlighting, until the Find First button selects the whole text.
Alternatively, you can use the anchors \A and \Z at the start and the end of your regex to force it to match the whole string. When you do that, your regex always behaves in the same way, whether you test it in RegexBuddy, or whether you use matches() or another method in Java. Only matches() requires a full string match. All other Matcher methods in Java allow partial matches.
正则表达式
将为您提供您所描述的项目,但仅按照您所描述的顺序,并且每一项都可以根据需要提供任意数量。 所以“skjhsklasdkjgsh((((())))))&&&&&.....”有效,但不混合字符。
您需要一个像这样的正则表达式:
它将允许混合所有字符。
编辑:我的正则表达式知识有限,所以上面的语法可能并不完美。
the regex
will give you the items you described, but only in the order you described, and each one can be as many as wanted. So "skjhsklasdkjgsh((((())))))&&&&&....." works, but not mixing the characters.
You want a regex like this:
which will allow a mix of all characters.
edit: my regex knowledge is limited, so the above syntax may not be perfect.