gpt4 book ai didi

java - 正则表达式:我想使用正则表达式来操作/更改特定短语。

转载 作者:行者123 更新时间:2023-12-01 12:07:03 25 4
gpt4 key购买 nike

我有以下字符串/文本:

It was very hot today, even by local standards, and the only other fossils we found in the quarry during the rest of our searching were small, unidentifiable fragments of bone. The plan for tomorrow is to do more prospecting for fossils in a couple of other quarries in the area.

我需要遵守这些规则:

  • 所有大于 2 个字符的单词应在第二个字符后被截断,并添加字符串“-xx”后缀;例如英语单词“the”变成“th-xx”
  • 必须保持大写
  • 标点符号必须保留,除非它出现在要截断的单词的一部分

我想使用某种正则表达式来扫描文本并根据上述规则进行更改。这是在 Android/Java 上,任何建议将不胜感激。

最佳答案

您可以使用:

String repl = input.replaceAll("\\b([a-zA-Z]{2})[a-zA-Z]+\\b", "$1-xx");

RegEx Demo

关于java - 正则表达式:我想使用正则表达式来操作/更改特定短语。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27520229/

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