gpt4 book ai didi

java - 简单的正则表达式匹配

转载 作者:行者123 更新时间:2023-11-30 04:47:16 25 4
gpt4 key购买 nike

我想创建一个正则表达式,它生成一个匹配项,并删除 $ 和最后两个字符(如果最后一个字符以大写字母加数字结尾)。

I'll strip off the $ and then an ending capital letter + number:

$mytestA1 --> expected output: mytest
$againD4 --> expected output: again
$something --> expected output: something
$name3 --> expected output: name3 // because there was no capital letter before the number digit
$name2P4 --> expected output: name2

在我什至懒得运行正则表达式之前,我将在代码中进行“if”检查,检查 $ 是否存在。

谢谢。

最佳答案

在 Java 中只需使用 String#replaceAll:

String replaced = str.replaceAll("^\\$|[A-Z]\\d$", "");

关于java - 简单的正则表达式匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10692087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com