mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
ReUtil类添加delLast方法✒️
This commit is contained in:
parent
4f634c384d
commit
db42db25da
@ -299,6 +299,46 @@ public class ReUtil {
|
||||
return pattern.matcher(content).replaceFirst(StrUtil.EMPTY);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除匹配的最后一个内容
|
||||
*
|
||||
* @param regex 正则
|
||||
* @param str 被匹配的内容
|
||||
* @return 删除后剩余的内容
|
||||
* @since 5.6.5
|
||||
*/
|
||||
public static String delLast(String regex, CharSequence str) {
|
||||
if (StrUtil.hasBlank(regex, str)) {
|
||||
return StrUtil.str(str);
|
||||
}
|
||||
|
||||
final Pattern pattern = PatternPool.get(regex, Pattern.DOTALL);
|
||||
return delLast(pattern, str);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除匹配的最后一个内容
|
||||
*
|
||||
* @param pattern 正则
|
||||
* @param str 被匹配的内容
|
||||
* @return 删除后剩余的内容
|
||||
* @since 5.6.5
|
||||
*/
|
||||
public static String delLast(Pattern pattern, CharSequence str) {
|
||||
if (null != pattern && StrUtil.isNotBlank(str)) {
|
||||
String last = "";
|
||||
for (Matcher matcher = pattern.matcher(str); matcher.find(); ) {
|
||||
last = matcher.group();
|
||||
}
|
||||
|
||||
if (StrUtil.isNotBlank(last)){
|
||||
return StrUtil.subBefore(str, last, Boolean.TRUE) + StrUtil.subAfter(str, last, Boolean.TRUE);
|
||||
}
|
||||
}
|
||||
|
||||
return StrUtil.str(str);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除匹配的全部内容
|
||||
*
|
||||
@ -724,4 +764,4 @@ public class ReUtil {
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package cn.hutool.core.util;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.lang.PatternPool;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
@ -23,7 +24,7 @@ public class ReUtilTest {
|
||||
String resultExtractMulti = ReUtil.extractMulti("(\\w)aa(\\w)", content, "$1-$2");
|
||||
Assert.assertEquals("Z-a", resultExtractMulti);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void extractMultiTest2() {
|
||||
// 抽取多个分组然后把它们拼接起来
|
||||
@ -37,7 +38,32 @@ public class ReUtilTest {
|
||||
String resultDelFirst = ReUtil.delFirst("(\\w)aa(\\w)", content);
|
||||
Assert.assertEquals("ZZbbbccc中文1234", resultDelFirst);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void delLastTest(){
|
||||
String blank = "";
|
||||
String word = "180公斤";
|
||||
String sentence = "10.商品KLS100021型号xxl适合身高180体重130斤的用户";
|
||||
//空字符串兼容
|
||||
Assert.assertEquals(blank,ReUtil.delLast("\\d+", blank));
|
||||
Assert.assertEquals(blank,ReUtil.delLast(PatternPool.NUMBERS, blank));
|
||||
|
||||
//去除数字
|
||||
Assert.assertEquals("公斤",ReUtil.delLast("\\d+", word));
|
||||
Assert.assertEquals("公斤",ReUtil.delLast(PatternPool.NUMBERS, word));
|
||||
//去除汉字
|
||||
Assert.assertEquals("180",ReUtil.delLast("[\u4E00-\u9FFF]+", word));
|
||||
Assert.assertEquals("180",ReUtil.delLast(PatternPool.CHINESES, word));
|
||||
|
||||
//多个匹配删除最后一个 判断是否不在包含最后的数字
|
||||
Assert.assertFalse(ReUtil.delLast("\\d+", sentence).contains("130"));
|
||||
Assert.assertFalse(ReUtil.delLast(PatternPool.NUMBERS, sentence).contains("130"));
|
||||
|
||||
//多个匹配删除最后一个 判断是否不在包含最后的数字
|
||||
Assert.assertFalse(ReUtil.delLast("[\u4E00-\u9FFF]+", sentence).contains("斤的用户"));
|
||||
Assert.assertFalse(ReUtil.delLast(PatternPool.CHINESES, sentence).contains("斤的用户"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void delAllTest() {
|
||||
// 删除所有匹配到的内容
|
||||
@ -60,14 +86,14 @@ public class ReUtilTest {
|
||||
Integer resultGetFirstNumber = ReUtil.getFirstNumber(content);
|
||||
Assert.assertEquals(Integer.valueOf(1234), resultGetFirstNumber);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void isMatchTest() {
|
||||
// 给定字符串是否匹配给定正则
|
||||
boolean isMatch = ReUtil.isMatch("\\w+[\u4E00-\u9FFF]+\\d+", content);
|
||||
Assert.assertTrue(isMatch);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void replaceAllTest() {
|
||||
//通过正则查找到字符串,然后把匹配到的字符串加入到replacementTemplate中,$1表示分组1的字符串
|
||||
@ -75,43 +101,43 @@ public class ReUtilTest {
|
||||
String replaceAll = ReUtil.replaceAll(content, "(\\d+)", "->$1<-");
|
||||
Assert.assertEquals("ZZZaaabbbccc中文->1234<-", replaceAll);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void replaceAllTest2() {
|
||||
//此处把1234替换为 ->1234<-
|
||||
String replaceAll = ReUtil.replaceAll(this.content, "(\\d+)", parameters -> "->" + parameters.group(1) + "<-");
|
||||
Assert.assertEquals("ZZZaaabbbccc中文->1234<-", replaceAll);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void replaceTest() {
|
||||
String str = "AAABBCCCBBDDDBB";
|
||||
String replace = StrUtil.replace(str, 0, "BB", "22", false);
|
||||
Assert.assertEquals("AAA22CCC22DDD22", replace);
|
||||
|
||||
|
||||
replace = StrUtil.replace(str, 3, "BB", "22", false);
|
||||
Assert.assertEquals("AAA22CCC22DDD22", replace);
|
||||
|
||||
|
||||
replace = StrUtil.replace(str, 4, "BB", "22", false);
|
||||
Assert.assertEquals("AAABBCCC22DDD22", replace);
|
||||
|
||||
|
||||
replace = StrUtil.replace(str, 4, "bb", "22", true);
|
||||
Assert.assertEquals("AAABBCCC22DDD22", replace);
|
||||
|
||||
|
||||
replace = StrUtil.replace(str, 4, "bb", "", true);
|
||||
Assert.assertEquals("AAABBCCCDDD", replace);
|
||||
|
||||
|
||||
replace = StrUtil.replace(str, 4, "bb", null, true);
|
||||
Assert.assertEquals("AAABBCCCDDD", replace);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void escapeTest() {
|
||||
//转义给定字符串,为正则相关的特殊符号转义
|
||||
String escape = ReUtil.escape("我有个$符号{}");
|
||||
Assert.assertEquals("我有个\\$符号\\{\\}", escape);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getAllGroupsTest() {
|
||||
//转义给定字符串,为正则相关的特殊符号转义
|
||||
@ -121,7 +147,7 @@ public class ReUtilTest {
|
||||
Assert.assertEquals("192", allGroups.get(1));
|
||||
Assert.assertEquals("168", allGroups.get(2));
|
||||
Assert.assertEquals("1", allGroups.get(3));
|
||||
|
||||
|
||||
allGroups = ReUtil.getAllGroups(pattern, "192-168-1-1", false);
|
||||
Assert.assertEquals("192", allGroups.get(0));
|
||||
Assert.assertEquals("168", allGroups.get(1));
|
||||
|
Loading…
x
Reference in New Issue
Block a user