正则表达式算法用于过滤复杂字符串

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

Regex algorithm to filter complex strings

问题

考虑到这个 SQL 字符串(无效的 SQL):

SELECT * FROM venue AS v, date AS d 
WHERE "v"."venueId" = '1' 
AND "v"."<PREFIX>d.dateId" = '102' 
AND "v"."<PREFIX>d.dateTime" = '12345678'
LIMIT 5;

我们如何通过将"v"."<PREFIX> 转换为 "d.dateId" 来修复无效的 SQL:

  • "v"."<PREFIX> 已被移除
  • 可以上面插入一个唯一的 PREFIX(例如)来帮助过滤

期望的有效 SQL:

SELECT * FROM venue AS v, date AS d
WHERE "v"."venueId" = '1' 
AND "d"."dateId" = '102' 
AND "d"."dateTime" = '12345678'
LIMIT 5;

或者备用的有效 SQL(在 WHERE 条件中没有用于列alias.columnName的双引号):

SELECT * FROM venue AS v, date AS d 
WHERE v.venueId = '1' 
AND d.dateId = '102' 
AND d.dateTime = '12345678'
LIMIT 5;
英文:

Consider this SQL string (Invalid SQL)

SELECT * FROM venue AS v, date AS d 
WHERE "v"."venueId" = '1' 
AND "v"."<PREFIX>d.dateId" = '102' 
AND "v"."<PREFIX>d.dateTime" = '12345678'
LIMIT 5;

How can we fix the invalid SQL by transforming e.g.
"v"."<PREFIX>d.dateId" into "d.dateId"

  • "v"."<PREFIX> was removed
  • An unique PREFIX can be inserted above (example) to help with the filtering

Expected valid SQL:

SELECT * FROM venue AS v, date AS d
WHERE "v"."venueId" = '1' 
AND "d"."dateId" = '102' 
AND "d"."dateTime" = '12345678'
LIMIT 5;

Or alternative valid SQL (without double quotes for column alias.columnName in WHERE conditions)

SELECT * FROM venue AS v, date AS d 
WHERE v.venueId = '1' 
AND d.dateId = '102' 
AND d.dateTime = '12345678'
LIMIT 5;

答案1

得分: 1

以下是翻译好的部分:

你不真的需要在匹配中包含这个前缀,因为匹配涉及到第二对引号,其中包括一个点:

    const badsql = `SELECT * FROM venue AS v, date AS d 
    WHERE "v"."venueId" = '1' 
    AND "v"."d.dateId" = '102' 
    AND "v"."d.dateTime" = '12345678'
    LIMIT 5;`;

    const sql = badsql.replace(/"[^"]+"\.("[^."]+)\./g, '$1"."');

    console.log(sql);
英文:

You don't really need to have that prefix as the match concerns a second pair of quotes that includes a point:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const badsql = `SELECT * FROM venue AS v, date AS d 
WHERE &quot;v&quot;.&quot;venueId&quot; = &#39;1&#39; 
AND &quot;v&quot;.&quot;d.dateId&quot; = &#39;102&#39; 
AND &quot;v&quot;.&quot;d.dateTime&quot; = &#39;12345678&#39;
LIMIT 5;`;

const sql = badsql.replace(/&quot;[^&quot;]+&quot;\.(&quot;[^.&quot;]+)\./g, &#39;$1&quot;.&quot;&#39;);

console.log(sql);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月14日 21:18:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448422.html
匿名

发表评论

匿名网友

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

确定