正则表达式删除引号

时间:2013-06-14 04:30:59

标签: java regex

如何编写正则表达式来满足这些要求?我只能使用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 ? 

2 个答案:

答案 0 :(得分:7)

描述

此正则表达式将执行以下操作:

  1. 如果有2个初始“字符串和结尾”,则删除单个“
  2. 如果1个首字母“字符串和结尾”,则删除任何内容
  3. 如果0个初始“字符串和结尾”,则删除结尾”
  4. 正则表达式:^(?=.*?”)“\s*(“)|^(?=.*?”)(“.*?”)|^(?!“)(.*?)”

    替换为:$1$2$3

    enter image description here

    输入文字

    “ DO NOTHING  ”
    “ “ REMOVE INITIAL LD  ”
    REMOVE RD  ”
    

    重新输出文字

    “ DO NOTHING  ”
    “ REMOVE INITIAL LD ”
    REMOVE RD
    

    这些表达式从聊天会话中删除,并写入以A,B,C顺序一次执行一个,但是因为它们是分开的,所以它们可以按照开发人员希望改变的任何顺序执行基于所需的输出。

    A

    • 1 LD和1 RD,删除RD
    • 2 LD和1 RD,什么都不做
    • 正则表达式:^(“(?!\s*“).*?)”
    • 替换为$1

    B

    • 1 LD,删除1 LD
    • 2 LD,删除1 LD
    • 正则表达式:^“(\s*(?:“)?)
    • 替换为$1

    C

    • 1 LD和1 RD,什么都不做
    • 0 LD和1 RD,删除RD
    • 正则表达式:^(?!“)(.*?)”
    • 替换为$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图片:

Regular expression image