在字符串的特定部分之前和之后,将转义的单引号替换为普通的单引号。

huangapple go评论77阅读模式
英文:

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 word LOOP
  • .*? - any zero or more chars as few as possible
  • ^ - start of a line
  • END LOOP\b - whole word END 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

huangapple
  • 本文由 发表于 2020年10月6日 02:20:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/64214125.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定