mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix bug
This commit is contained in:
parent
83a4f14b43
commit
6f6e1e2fd5
25
hutool-core/src/main/java/cn/hutool/core/comparator/StrLengthComparator.java
Executable file
25
hutool-core/src/main/java/cn/hutool/core/comparator/StrLengthComparator.java
Executable file
@ -0,0 +1,25 @@
|
|||||||
|
package cn.hutool.core.comparator;
|
||||||
|
|
||||||
|
import java.util.Comparator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 字符串长度比较器,短在前
|
||||||
|
*
|
||||||
|
* @author looly
|
||||||
|
* @since 5.8.9
|
||||||
|
*/
|
||||||
|
public class StrLengthComparator implements Comparator<CharSequence> {
|
||||||
|
/**
|
||||||
|
* 单例的字符串长度比较器,短在前
|
||||||
|
*/
|
||||||
|
public static final StrLengthComparator INSTANCE = new StrLengthComparator();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compare(final CharSequence o1, final CharSequence o2) {
|
||||||
|
int result = Integer.compare(o1.length(), o2.length());
|
||||||
|
if (0 == result) {
|
||||||
|
result = CompareUtil.compare(o1.toString(), o2.toString());
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
package cn.hutool.core.regex;
|
package cn.hutool.core.regex;
|
||||||
|
|
||||||
import cn.hutool.core.collection.SetUtil;
|
import cn.hutool.core.collection.SetUtil;
|
||||||
|
import cn.hutool.core.comparator.StrLengthComparator;
|
||||||
import cn.hutool.core.convert.Convert;
|
import cn.hutool.core.convert.Convert;
|
||||||
import cn.hutool.core.lang.Assert;
|
import cn.hutool.core.lang.Assert;
|
||||||
import cn.hutool.core.lang.Validator;
|
import cn.hutool.core.lang.Validator;
|
||||||
@ -878,7 +879,8 @@ public class ReUtil {
|
|||||||
final Matcher matcher = pattern.matcher(content);
|
final Matcher matcher = pattern.matcher(content);
|
||||||
boolean result = matcher.find();
|
boolean result = matcher.find();
|
||||||
if (result) {
|
if (result) {
|
||||||
final Set<String> varNums = findAll(PatternPool.GROUP_VAR, replacementTemplate, 1, new HashSet<>());
|
final Set<String> varNums = findAll(PatternPool.GROUP_VAR, replacementTemplate, 1,
|
||||||
|
new TreeSet<>(StrLengthComparator.INSTANCE.reversed()));
|
||||||
final StringBuffer sb = new StringBuffer();
|
final StringBuffer sb = new StringBuffer();
|
||||||
do {
|
do {
|
||||||
String replacement = replacementTemplate;
|
String replacement = replacementTemplate;
|
||||||
|
@ -204,4 +204,12 @@ public class ReUtilTest {
|
|||||||
Assert.assertEquals(map.get("month"), "10");
|
Assert.assertEquals(map.get("month"), "10");
|
||||||
Assert.assertEquals(map.get("day"), "11");
|
Assert.assertEquals(map.get("day"), "11");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void issuesI5TQDRTest(){
|
||||||
|
final Pattern patternIp = Pattern.compile("((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})\\.((2(5[0-5]|[0-4]\\d))"
|
||||||
|
+ "|[0-1]?\\d{1,2})\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})");
|
||||||
|
final String s = ReUtil.replaceAll("1.2.3.4", patternIp, "$1.**.**.$10");
|
||||||
|
Assert.assertEquals("1.**.**.4", s);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user