如何编写正则表达式来满足这些要求?我只能使用string.replaceAll函数..
a)”
显示在段落末尾的“
,而“ “
,但不是”
- 删除“
b)对于出现在段落开头的“
,请移除“ “
[注意:如果有“
,则现在应为”
]
c)对于“
显示在段落末尾没有匹配”
的段落{ - 1}}
编辑:
Rule a)
Transform:
String input1 ="“remove quotes”"
String output1 ="“remove quotes"
Don't change anything:
String input1 ="““remove quotes”"
String output1 ="““remove quotes”"
Rule b)
Transform:
String input1 ="“remove quotes”"
String output1 ="remove quotes”"
Replace with single ldquo:
String input1 ="““remove quotes”"
String output1 ="“remove quotes”"
Rule c)
Do nothing (there is a matching ldquo):
String input1 ="“do not remove quotes”"
String output1 ="“do not remove quotes”"
Transform(no matching ldquo hence remove rdquo):
String input1 ="remove quotes”"
String output1 ="remove quotes"
I think I am going to run all the 3 rules separately on the string. What would be 3 regexes and replace expressions ?
答案 0 :(得分:7)
此正则表达式将执行以下操作:
“
字符串和结尾”
,则删除单个“
“
字符串和结尾”
,则删除任何内容“
字符串和结尾”
,则删除结尾”
正则表达式:^(?=.*?”)“\s*(“)|^(?=.*?”)(“.*?”)|^(?!“)(.*?)”
替换为:$1$2$3
输入文字
“ DO NOTHING ”
“ “ REMOVE INITIAL LD ”
REMOVE RD ”
重新输出文字
“ DO NOTHING ”
“ REMOVE INITIAL LD ”
REMOVE RD
这些表达式从聊天会话中删除,并写入以A,B,C顺序一次执行一个,但是因为它们是分开的,所以它们可以按照开发人员希望改变的任何顺序执行基于所需的输出。
A
^(“(?!\s*“).*?)”
$1
B
^“(\s*(?:“)?)
$1
C
^(?!“)(.*?)”
$1
答案 1 :(得分:0)
如果我理解得很好,字符串如:
“ Criteria 1, ending with RD and beginning with LD, but not LDLD, remove RD ”
“ “ Criteria 1, ending with RD but beginning with LDLD, do nothing to RD ”
“ “ Criteria 2, beginning with LDLD, make it begin with LD ”
Criteria 3 with non-matching RD, remove RD ”
成为:
“ Criteria 1, ending with RD and beginning with LD, but not LDLD, remove RD
“ Criteria 1, ending with RD but beginning with LDLD, do nothing to RD ”
“ Criteria 2, beginning with LDLD, make it begin with LD ”
Criteria 3 with non-matching RD, remove RD
您可以使用正则表达式:
^(?:(“(?! “).*?)\s*”|(“) “(.*)|((?!“).*?)\s*”)$
并替换为$1$2$3$4
。
了解它的工作原理here。
或者如果你的意思是符号,你可以找到另一个类似的here。
“ Criteria 1, ending with RD and beginning with LD, but not LDLD, remove RD ”
“ “ Criteria 1, ending with RD but beginning with LDLD, do nothing to RD ”
“ “ Criteria 2, beginning with LDLD, make it begin with LD ”
Criteria 3 with non-matching RD, remove RD ”
成为:
“ Criteria 1, ending with RD and beginning with LD, but not LDLD, remove RD
“ Criteria 1, ending with RD but beginning with LDLD, do nothing to RD ”
“ Criteria 2, beginning with LDLD, make it begin with LD ”
Criteria 3 with non-matching RD, remove RD
如果你想调试可能使正则表达式更容易理解的debuggex图片: