英文:
Java match two strings without last character
问题
Sure, here's the translated content:
我有一个带有路径的URL,路径为/mypath/check/10.10/-123.11
。如果(可选地)小数点后面有3位数字而不是2位,我想要返回true,例如/mypath/check/10.101/-123.112
在匹配时应返回true。小数点前对于这两种情况都应完全匹配。
举些例子:
成功案例:
/mypath/check/10.10/-123.11
=/mypath/check/10.101/-123.112
/mypath/check/10.10/-123.11
=/mypath/check/10.101/-123.11
/mypath/check/10.10/-123.11
=/mypath/check/10.10/-123.112
/mypath/check/10.10/123.11
=/mypath/check/10.101/123.112
等等……
失败案例:
/mypath/check/10.10/-123.11
!=/mypath/check/10.121/-123.152
/mypath/check/10.11/-123.11
!=/mypath/check/10.12/-123.11
小数点前的数字可以包括1到3个数字的带有-
的数字。
英文:
I've a URL with path being /mypath/check/10.10/-123.11
. I want to return true if (optionally) there are 3 digits after decimal instead of 2 e.g /mypath/check/10.101/-123.112
should return true when matched. Before decimal for both two occurences should be exact match.
To cite some examples :
Success
/mypath/check/10.10/-123.11 = /mypath/check/10.101/-123.112
/mypath/check/10.10/-123.11 = /mypath/check/10.101/-123.11
/mypath/check/10.10/-123.11 = /mypath/check/10.10/-123.112
/mypath/check/10.10/123.11 = /mypath/check/10.101/123.112
.. and so forth
Failure :
/mypath/check/10.10/-123.11 != /mypath/check/10.121/-123.152
/mypath/check/10.11/-123.11 != /mypath/check/10.12/-123.11
The numbers before decimal can include -
with digits with 1 to 3 numbers.
答案1
得分: 0
Idea
使用正则表达式子模式,带有?
修饰符的部分将匹配可选项。在您的情况下,这适用于小数点后的第3个字符。
可以通过匹配上下文模式的每个出现,并将匹配中的可选部分替换为空字符串来实现对该可选数字取模的等式测试。在进行此规范化后,可以测试字符串的相等性。
Code
// 初始化测试数据。
// 将batch1、batch2中相同位置的字符串进行比较。
//
String[] batch1 = {
"/mypath/check/10.10/-123.11",
"/mypath/check/10.10/-123.11",
"/mypath/check/10.10/-123.11",
"/mypath/check/10.10/123.11",
"/mypath/check/10.10/-123.11",
"/mypath/check/10.11/-123.11"
};
String[] batch2 = {
"/mypath/check/10.101/-123.112",
"/mypath/check/10.101/-123.11",
"/mypath/check/10.10/-123.112",
"/mypath/check/10.101/123.112",
"/mypath/check/10.121/-123.152",
"/mypath/check/10.12/-123.11"
};
// 用于规范化的正则表达式模式:
// - 基本模式:小数点后面跟着2或3位数字
// - 可选部分:基本模式的第3位数字
// - 额外上下文:模式必须在字符串末尾匹配,或者后面跟着一个非数字字符。
//
Pattern re_p = Pattern.compile("([.][0-9]{2})[0-9]?(?:$|(?![0-9]))");
// 用于处理正则匹配的替换程序。返回捕获组#1
Function<MatchResult, String> fnReplacer = (MatchResult m) -> {
return m.group(1);
};
// 处理每个测试案例
// 预期结果
// 匹配
// 匹配
// 匹配
// 匹配
// 不匹配
// 不匹配
//
for (int i = 0; i < batch1.length; i++) {
String norm1 = re_p.matcher(batch1[i]).replaceAll(fnReplacer);
String norm2 = re_p.matcher(batch2[i]).replaceAll(fnReplacer);
if (norm1.equals(norm2)) {
System.out.println("Url pair #" + Integer.toString(i) + ": match ( '" + norm1 + "' == '" + norm2 + "' )");
} else {
System.out.println("Url pair #" + Integer.toString(i) + ": mismatch ( '" + norm1 + "' != '" + norm2 + "' )");
}
}
演示可在此处查看(ideone.com)。
英文:
Idea
Regex subpatterns that shall match optionally are suffixed with the ?
modifier. In your case this applies to the 3rd character after a decimal point.
An equality tests modulo that optional digit may be implemented in matching each occurrence of the context pattern and replacing the optional part within the match with the empty string. After this normalization the strings can be tested for equality.
Code
// Initializing test data.
// Will compare Strings in batch1, batch2 at the same array position.
//
String[] batch1 = {
"/mypath/check/10.10/-123.11"
, "/mypath/check/10.10/-123.11"
, "/mypath/check/10.10/-123.11"
, "/mypath/check/10.10/123.11"
, "/mypath/check/10.10/-123.11"
, "/mypath/check/10.11/-123.11"
};
String[] batch2 = {
"/mypath/check/10.101/-123.112"
, "/mypath/check/10.101/-123.11"
, "/mypath/check/10.10/-123.112"
, "/mypath/check/10.101/123.112"
, "/mypath/check/10.121/-123.152"
, "/mypath/check/10.12/-123.11"
};
// Regex pattern used for normalization:
// - Basic pattern: decimal point followed by 2 or 3 digits
// - Optional part: 3rd digit of the basic pattern
// - Additional context: Pattern must match at the end of the string or be followed by a non-digit character.
//
Pattern re_p = Pattern.compile("([.][0-9]{2})[0-9]?(?:$|(?![0-9]))");
// Replacer routine for processing the regex match. Returns capture group #1
Function<MatchResult, String> fnReplacer= (MatchResult m)-> { return m.group(1); };
// Processing each test case
// Expected result
// match
// match
// match
// match
// mismatch
// mismatch
//
for ( int i = 0; i < batch1.length; i++ ) {
String norm1 = re_p.matcher(batch1[i]).replaceAll(fnReplacer);
String norm2 = re_p.matcher(batch2[i]).replaceAll(fnReplacer);
if (norm1.equals(norm2)) {
System.out.println("Url pair #" + Integer.toString(i) + ": match ( '" + norm1 + "' == '" + norm2 + "' )");
} else {
System.out.println("Url pair #" + Integer.toString(i) + ": mismatch ( '" + norm1 + "' != '" + norm2 + "' )");
}
}
Demo available here (ideone.com).
答案2
得分: 0
尝试 /mypath/check/10\.10/-?123\.11[ ]*=[ ]*/mypath/check/(\d\d)\.\1\d?/
demo
英文:
Try /mypath/check/10\.10/-?123\.11[ ]*=[ ]*/mypath/check/(\d\d)\.\1\d?/
demo
答案3
得分: 0
尝试这样做:
url1.equals(url2) || url1.equals(url2.replaceAll("\\d$", ""))
英文:
Try this:
url1.equals(url2) || url1.equals(url2.replaceAll("\\d$", ""))
答案4
得分: 0
以下是翻译好的代码部分:
static boolean matchURL(String url1, String url2)
{
return url2.matches(url1.replaceAll("([.][0-9]{2})", "$1[0-9]?"));
}
Test:
String url1 = "/mypath/check/10.10/-123.11";
List<String> tests = Arrays.asList(
"/mypath/check/10.10/-123.11",
"/mypath/check/10.10/-123.111",
"/mypath/check/10.101/-123.11",
"/mypath/check/10.101/-123.111",
"/mypath/check/10.11/-123.11"
);
for(String url2 : tests)
System.out.format("%s : %s = %b%n", url1, url2, matchURL(url1, url2));
Output:
/mypath/check/10.10/-123.11 : /mypath/check/10.10/-123.11 = true
/mypath/check/10.10/-123.11 : /mypath/check/10.10/-123.111 = true
/mypath/check/10.10/-123.11 : /mypath/check/10.101/-123.11 = true
/mypath/check/10.10/-123.11 : /mypath/check/10.101/-123.111 = true
/mypath/check/10.10/-123.11 : /mypath/check/10.11/-123.11 = false
英文:
I'm assuming the first URL always has exactly 2 digits after every decimal point. If so, match the 2nd URL to the regex formed by appending an optional digit to the end of each decimal fraction in the first URL.
static boolean matchURL(String url1, String url2)
{
return url2.matches(url1.replaceAll("([.][0-9]{2})", "$1[0-9]?"));
}
Test:
String url1 = "/mypath/check/10.10/-123.11";
List<String> tests = Arrays.asList(
"/mypath/check/10.10/-123.11",
"/mypath/check/10.10/-123.111",
"/mypath/check/10.101/-123.11",
"/mypath/check/10.101/-123.111",
"/mypath/check/10.11/-123.11"
);
for(String url2 : tests)
System.out.format("%s : %s = %b%n", url1, url2, matchURL(url1, url2));
Output:
/mypath/check/10.10/-123.11 : /mypath/check/10.10/-123.11 = true
/mypath/check/10.10/-123.11 : /mypath/check/10.10/-123.111 = true
/mypath/check/10.10/-123.11 : /mypath/check/10.101/-123.11 = true
/mypath/check/10.10/-123.11 : /mypath/check/10.101/-123.111 = true
/mypath/check/10.10/-123.11 : /mypath/check/10.11/-123.11 = false
专注分享java语言的经验与见解,让所有开发者获益!
评论