4

如何编写正则表达式来满足这些要求?我只能使用 string.replaceAll 函数..

a) For ”which 出现在段落末尾有一个“, 但不是“ “-remove”

b)对于“出现在段落开头的内容删除 “ [注意:如果有“ “,现在应该是“]

c)对于”出现在段落末尾而在段落“开头没有匹配的内容 - 删除”

编辑:

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 ? 
4

2 回答 2

7

Description

This regex will do the following:

  1. if 2 initial “ strings and a ending ”, then remove single “
  2. if 1 initial “ string and a ending ”, then remove nothing
  3. if 0 initial “ strings and a ending ”, then remove ending ”

regex: ^(?=.*?”)“\s*(“)|^(?=.*?”)(“.*?”)|^(?!“)(.*?)”

replace with: $1$2$3

enter image description here

Input text

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

Output text respecitivly

“ DO NOTHING  ”
“ REMOVE INITIAL LD ”
REMOVE RD

These expressions where hashed out from a chat session, and written to be executed one at a time in A,B,C order, however because they are seperate, they can be executed in any order the developer would like which would change based on the desired output.

A

  • 1 LD and 1 RD, remove the RD
  • 2 LD and 1 RD, do nothing
  • regex: ^(“(?!\s*“).*?)”
  • replace with $1

B

  • 1 LD, remove 1 LD
  • 2 LD, remove 1 LD
  • regex: ^“(\s*(?:“)?)
  • replace with $1

C

  • 1 LD and 1 RD, do nothing
  • 0 LD and 1 RD, remove the RD
  • regex: ^(?!“)(.*?)”
  • replace with $1
于 2013-06-14T04:59:34.857 回答
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.

看看它是如何工作

或者,如果您指的是符号,您可以在此处找到另一个类似的符号。

“ 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

如果您想要调试表达式图片,这可能会使正则表达式更易于理解:

正则表达式图片

于 2013-06-14T07:45:48.950 回答