![](/img/trans.png)
[英]Java - Need some help on a Regex pattern for matching both http and https
[英]Need help in regex matching
这可能很简单,但是我对regex还是非常陌生,并且有一个要求,我需要在字符串中进行一些regex匹配并提取其中的数字。 以下是我的代码,其中包含示例i / p和必需的o / p。 我尝试通过参考https://www.freeformatter.com/java-regex-tester.html构造Pattern
,但是我的正则表达式匹配项本身返回false。
Pattern pattern = Pattern.compile(".*/(a-b|c-d|e-f)/([0-9])+(#[0-9]?)");
String str = "foo/bar/Samsung-Galaxy/a-b/1"; // need to extract 1.
String str1 = "foo/bar/Samsung-Galaxy/c-d/1#P2";// need to extract 2.
String str2 = "foo.com/Samsung-Galaxy/9090/c-d/69"; // need to extract 69
System.out.println("result " + pattern.matcher(str).matches());
System.out.println("result " + pattern.matcher(str1).matches());
System.out.println("result " + pattern.matcher(str1).matches());
以上所有SOP都返回false。 我正在使用Java 8,请问有什么方法可以在单个语句中匹配模式,然后从字符串中提取数字 。
如果有人可以指出如何调试/开发正则表达式,我将非常高兴。如果我的问题不清楚,请随时告诉我。
您可以使用
Pattern pattern = Pattern.compile(".*/(?:a-b|c-d|e-f)/[^/]*?([0-9]+)");
与matches()
,上面的模式不需要显式锚点^
和$
。
细节
.*
-尽可能多的除换行符以外的0+个字符 /
-最右边的/
,后跟子模式 (?:ab|cd|ef)
-一个与内部任何替代匹配的非捕获组: ab
, cd
或ef
/
-一/
炭 [^/]*?
- /
以外的任何字符,请尽可能少 ([0-9]+)
-组1:一位或多位数字。 Java演示 :
List<String> strs = Arrays.asList("foo/bar/Samsung-Galaxy/a-b/1","foo/bar/Samsung-Galaxy/c-d/1#P2","foo.com/Samsung-Galaxy/9090/c-d/69");
Pattern pattern = Pattern.compile(".*/(?:a-b|c-d|e-f)/[^/]*?([0-9]+)");
for (String s : strs) {
Matcher m = pattern.matcher(s);
if (m.matches()) {
System.out.println(s + ": \"" + m.group(1) + "\"");
}
}
使用带有锚的相同正则表达式的替换方法:
List<String> strs = Arrays.asList("foo/bar/Samsung-Galaxy/a-b/1","foo/bar/Samsung-Galaxy/c-d/1#P2","foo.com/Samsung-Galaxy/9090/c-d/69");
String pattern = "^.*/(?:a-b|c-d|e-f)/[^/]*?([0-9]+)$";
for (String s : strs) {
System.out.println(s + ": \"" + s.replaceFirst(pattern, "$1") + "\"");
}
参见另一个Java演示 。
输出:
foo/bar/Samsung-Galaxy/a-b/1: "1"
foo/bar/Samsung-Galaxy/c-d/1#P2: "2"
foo.com/Samsung-Galaxy/9090/c-d/69: "69"
因为您总是匹配正则表达式中的最后一个数字,所以我想只将replaceAll
与此正则表达式.*?(\\d+)$
:
String regex = ".*?(\\d+)$";
String strResult1 = str.replaceAll(regex, "$1");
System.out.println(!strResult1.isEmpty() ? "result " + strResult1 : "no result");
String strResult2 = str1.replaceAll(regex, "$1");
System.out.println(!strResult2.isEmpty() ? "result " + strResult2 : "no result");
String strResult3 = str2.replaceAll(regex, "$1");
System.out.println(!strResult3.isEmpty() ? "result " + strResult3 : "no result");
如果结果为空,则您没有任何数字。
输出
result 1
result 2
result 69
这是使用String#replaceAll
:
public String getDigits(String input) {
String number = input.replaceAll(".*/(?:a-b|c-d|e-f)/[^/]*?(\\d+)$", "$1");
return number.matches("\\d+") ? number : "no match";
}
System.out.println(getDigits("foo.com/Samsung-Galaxy/9090/c-d/69"));
System.out.println(getDigits("foo/bar/Samsung-Galaxy/a-b/some other text/1"));
System.out.println(getDigits("foo/bar/Samsung-Galaxy/9090/a-b/69ace"));
69
no match
no match
这适用于您提供的样本输入。 请注意,我添加了逻辑,对于无法匹配您的模式的末尾数字,该逻辑将不显示no match
。 在不匹配的情况下,我们通常会留有原始输入字符串,而不是全数字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.