简体   繁体   English

Java字符串正则表达式匹配语法

[英]Java String Regex Matching Syntax

Can someone please explain why this won't work in Java but appears to in every other tool I have tested it on. 有人可以解释一下为什么这在Java中不起作用,但是在我测试过的所有其他工具中都如此。 The type of strings expected are similar to :a004871-5553:z05072-990 预期的字符串类型类似于:a004871-5553:z05072-990

The expected outcome is to match the a0 at the beginning of the line. 预期的结果是匹配行开头的a0

String[] parts = nextLine[0].split(":");
String part1 = parts[1]; 
if (part1.matches("a[0-9]"))....

This line is the problem: 这行是问题所在:

if (part1.matches("a[0-9]"))

Because String#matches matches the full input not just part of input 因为String#matches与完整输入匹配,而不仅仅是输入的一部分

This should work though: 这应该可以工作:

if (part1.matches("a[0-9-]+")) {...}

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

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