[英]Regex for numeric portion of Java string
I'm trying to write a Java method that will take a string as a parameter and return another string if it matches a pattern, and null
otherwise. 我正在尝试编写一个Java方法,该方法将字符串作为参数,并在匹配模式时返回另一个字符串,否则返回
null
。 The pattern: 模式:
:
"); :
”); then followed by Hence, some valid string thats match this pattern: 因此,一些与该模式匹配的有效字符串:
50: hello
1: d
10938484: 394958558
And some strings that do not match this pattern: 还有一些与该模式不匹配的字符串:
korfed49
: e4949
6
6:
6:sdjjd4
The general skeleton of the method is this: 该方法的一般框架是这样的:
public String extractNumber(String toMatch) {
// If toMatch matches the pattern, extract the first number
// (everything prior to the colon).
// Else, return null.
}
Here's my best attempt so far, but I know I'm wrong: 到目前为止,这是我最好的尝试,但是我知道我错了:
public String extractNumber(String toMatch) {
// If toMatch matches the pattern, extract the first number
// (everything prior to the colon).
String regex = "???";
if(toMatch.matches(regex))
return toMatch.substring(0, toMatch.indexOf(":"));
// Else, return null.
return null;
}
Thanks in advance. 提前致谢。
Your description is spot on, now it just needs to be translated to a regex: 您的描述很详细,现在只需将其翻译为正则表达式即可:
^ # Starts
\d+ # with a number (1+ digits); then followed by
: # A colon (":"); then followed by
# A single whitespace (" "); then followed by
\w+ # Any word character, one one more times
$ # (followed by the end of input)
Giving, in a Java string: 用Java字符串给出:
"^\\d+: \\w+$"
You also want to capture the numbers: put parentheses around \\d+
, use a Matcher
, and capture group 1 if there is a match: 您还想捕获数字:在
\\d+
加上括号,使用Matcher
,并在存在匹配项的情况下捕获组1:
private static final Pattern PATTERN = Pattern.compile("^(\\d+): \\w+$");
// ...
public String extractNumber(String toMatch) {
Matcher m = PATTERN.matcher(toMatch);
return m.find() ? m.group(1) : null;
}
Note: in Java, \\w
only matches ASCII characters and digits (this is not the case for .NET languages for instance) and it will also match an underscore. 注意:在Java中,
\\w
仅匹配ASCII字符和数字(例如,.NET语言不是这种情况),并且还将匹配下划线。 If you don't want the underscore, you can use (Java specific syntax): 如果您不想使用下划线,则可以使用(特定于Java的语法):
[\w&&[^_]]
instead of \\w
for the last part of the regex, giving: 而不是
\\w
作为正则表达式的最后一部分,给出:
"^(\\d+): [\\w&&[^_]]+$"
尝试使用以下命令:\\ d +:\\ w +
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.