Regular expression to remove quotes

How to write a regular expression to meet these requirements? I can only use the string.replaceAll function.

a) For ” that appears at the end of a paragraph that has “ but not “ “ “ “ -remove ”

b) For “ that appears at the beginning of the paragraph, delete “ [NOTE: if there is “ “ “ “ , now it should be “ ]

c) For ” that appears at the end of a paragraph without matching “ at the beginning of the paragraph -remove ”

EDIT:

 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
source share
2 answers

Description

This regex will do the following:

  • if 2 start lines “ and ending ” , then delete the single “
  • if 1 start line “ and ending ” and then delete nothing
  • if 0 initial “ lines and ending ” , then remove the termination ”

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

replace with: $1$2$3

enter image description here

Input text

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

Text output recursively

 “ DO NOTHING ” “ REMOVE INITIAL LD ” REMOVE RD 

These expressions that are hashed from the chat session and are written to be executed one by one in the order A, B, C, however, since they are separate, they can be executed in any order that the developer would like to change, based on the desired result.

A

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

B

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

C

  • 1 LD and 1 RD, do nothing
  • 0 LD and 1 RD, remove RD
  • regex: ^(?!“)(.*?)”
  • replace $1
+7
source

If I understand well, lines like:

 “ 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 ” 

To become:

 “ 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 

You can use regex:

 ^(?:("(?! ").*?)\s*"|(") "(.*)|((?!").*?)\s*")$ 

And replace with $1$2$3$4 .

See how it works here .

Or, if you meant characters, you can find something else similar 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 " 

Become:

 " 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 

And if you need a debuggex image that can make the regex more understandable:

Regular expression image

0
source

Source: https://habr.com/ru/post/1486162/


All Articles