How do I write a regular expression to satisfy these requirements ? I can only use a string.replaceAll function ..
a) For ”
which appears at end of paragraph which has a “
, but not “ “
“ “
—remove ”
b) For “
which appears at beginning of paragraph remove “
[NOTE: If there is “ “
“ “
, it should now be “
]
c) For ”
which appears at end of paragraph without a matching “
at beginning of 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 ?
This regex will do the following:
“
strings and a ending ”
, then remove single “
“
string and a ending ”
, then remove nothing “
strings and a ending ”
, then remove ending ”
regex: ^(?=.*?”)“\\s*(“)|^(?=.*?”)(“.*?”)|^(?!“)(.*?)”
replace with: $1$2$3
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
^(“(?!\\s*“).*?)”
$1
B
^“(\\s*(?:“)?)
$1
C
^(?!“)(.*?)”
$1
If I understand well, strings 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 the regex:
^(?:(“(?! “).*?)\s*”|(“) “(.*)|((?!“).*?)\s*”)$
And replace with $1$2$3$4
.
See how it works here .
Or if you meant the symbols, you can find another similar one 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 want the debuggex picture which might make the regex more understandable:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.