gpt4 book ai didi

Java 引用匹配

转载 作者:行者123 更新时间:2023-12-01 05:55:23 24 4
gpt4 key购买 nike

我不确定这是否是一个正则表达式问题,但我需要能够获取单引号内的内容,并用一些东西包围它们。例如:

this is a 'test' of 'something' and 'I' 'really' want 'to' 'solve this'

会变成

this is a ('test') of ('something') and ('I') ('really') want ('to') ('solve this')

您能提供的任何帮助都会很棒!

谢谢!

最佳答案

String str = "this is a 'test' of 'something'";
String rep = str.replaceAll("'[^']*'", "($0)"); // stand back, I know regex

我在这里所做的是使用 toe replaceAll() 方法,该方法搜索正则表达式 "'[^']*'" 的所有匹配项,并将它们替换为正则表达式 “($0)”

模式 "'[^']*'" 匹配所有以单引号 (') 开头和结尾的子字符串,并且它们之间可以是任何字符,除了另一个单引号 ([^']),并且这些单引号可以出现任意次数 (*)。将它们替换为 "($0)" 意味着获取每个匹配项 ($0) 并用括号括起来。

关于Java 引用匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3216947/

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