英文:
Replace escaped single quotes with normal single quotes before and after a certain part of string [Java/Groovy]
问题
Pattern pattern = Pattern.compile("(?i)\\bLOOP\\b");
Matcher matcher = pattern.matcher(body);
if (matcher.find()) {
int start = matcher.start();
int end = StringUtils.lastIndexOf(body, "END LOOP");
String beforeLoop = body.substring(0, start).replaceAll("''", "'");
String afterLoop = body.substring(end + 8).replaceAll("''", "'");
return beforeLoop + body.substring(start, end + 7) + afterLoop;
}
return body.replaceAll("''", "'");
英文:
How to efficiently replace escaped single quotes with normal single quotes before and after the "current" outermost loop. In my case single quotes are escaped like this : ''
The following input string :
"set x = ''value''; //BEFORE
Select
''L1: LOOP
set y = ''value2''
SELECT ''L2: LOOP...END LOOP L2;'';
END LOOP L1;''; //Cur OuterMost LOOP
if x = ''value'' then SELECT ''LEAVE L1''; //AFTER"
I want this Output. As you see, the body inside the outmost loop string is not being replaced i.e (''
-> '
)
"set x = 'value'; //BEFORE
Select
'L1: LOOP
set y = ''value2''
SELECT ''L2: LOOP...END LOOP L2;'';
END LOOP L1;'; //Cur OuterMost LOOP
if x = 'value' then SELECT 'LEAVE L1'; //AFTER"
I wrote the following code. It's doing the job but still, can I make it shorter and efficient?
Pattern pattern = Pattern.compile("(?i)\\bLOOP\\b")
Matcher matcher = pattern.matcher(body)
if (matcher.find()) {
int start = matcher.start()
int end = StringUtils.lastIndexOf(body, "END LOOP")
String beforeLoop = body[0..start - 1].replaceAll("''", "'")
String afterLoop = body[end + 8..-1].replaceAll("''", "'")
return beforeLoop + body[start..end + 7] + afterLoop
}
return body.replaceAll("''", "'")
答案1
得分: 1
String body = "set x = 'value'; //BEFORE\n Select \n'L1: LOOP\n set y = ''value2''\n SELECT ''L2: LOOP...END LOOP L2;'';\nEND LOOP L1;''; //Cur OuterMost LOOP\n if x = 'value' then SELECT 'LEAVE L1'; //AFTER"
body = body.replaceAll(/(?sm)\bLOOP\b.*?^END LOOP\b|('')/) { x,y ->
y != null ? "'" : x
}
print(body)
英文:
Assuming your outer loop ends with the leftmost END LOOP
at the start of a line, you can use a regex that matches the outer loops and matches and captures double '
chars so that you could "unescape" the value in Group 1 when it matches. The regex is
/(?sm)\bLOOP\b.*?^END LOOP\b|('')/
See the regex demo. Details:
(?sm)
- dot matches line breaks (s
) and^
now matches start of a line (m
)\bLOOP\b
- whole wordLOOP
.*?
- any zero or more chars as few as possible^
- start of a lineEND LOOP\b
- whole wordEND LOOP
|
- or('')
- Group 1:''
String body = "set x = ''value''; //BEFORE\n Select \n''L1: LOOP\n set y = ''value2''\n SELECT ''L2: LOOP...END LOOP L2;'';\nEND LOOP L1;''; //Cur OuterMost LOOP\n if x = ''value'' then SELECT ''LEAVE L1''; //AFTER"
body = body.replaceAll(/(?sm)\bLOOP\b.*?^END LOOP\b|('')/) { x,y ->
y != null ? "'" : x
}
print(body)
See the Groovy demo, output:
set x = 'value'; //BEFORE
Select
'L1: LOOP
set y = ''value2''
SELECT ''L2: LOOP...END LOOP L2;'';
END LOOP L1;'; //Cur OuterMost LOOP
if x = 'value' then SELECT 'LEAVE L1'; //AFTER
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论