如何在MySQL中用<span>标签替换括号中的字符串

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

How to replace strings in brackets with <span> tags in MySQL

问题

Sample:

在MySQL表中,我有数千个字符串,存储在名为query的varchar(4096)类型列中,它们总是类似于以下示例。括号的数量不总是相同的!这意味着在一个查询中可能会有3个括号中的变量,而在另一个查询中可能会有7个括号中的变量。

Expected result:

我想要做的是替换这些括号并将它们放在<span class="variable-1">中,其中1应该表示现有括号的数量:

How can we compare the total rewards <span class="variable-1">[team]</span>, taking into account that <span class="variable-2">[industry]</span> is better? Provide metrics for analysis, including <span class="variable-3">[costs]</span>, and <span class="variable-4">[best practices]</span>. Additionally suggest any <span class="variable-5">[potential areas for improvement]</span> based on your analysis.

How can i do this directly within MySQL?

英文:

I have thousands of string in a MySQL table in a column called query of type varchar(4096) which look always similar to the following one. The brackets are not always the same number! That means that in one query there could be 3 variables in brackets and in another on 7 variables in brackets.

Sample

“How can we compare the total rewards [team], taking into account that [industry] is better? Provide metrics for analysis, including [costs], and [best practices]. Additionally suggest any [potential areas for improvement] based on your analysis.”

What i want to do is to replace these brackets and put them in <span class="variable-1"> where 1 should be counting for the amount of existing brackets:

Expected result

“How can we compare the total rewards &lt;span class=&quot;variable-1&quot;&gt;[team]&lt;/span&gt;, taking into account that &lt;span class=&quot;variable-2&quot;&gt;[industry]&lt;/span&gt; is better? Provide metrics for analysis, including &lt;span class=&quot;variable-3&quot;&gt;[costs]&lt;/span&gt;, and &lt;span class=&quot;variable-4&quot;&gt;[best practices]&lt;/span&gt;. Additionally suggest any &lt;span class=&quot;variable-5&quot;&gt;[potential areas for improvement]&lt;/span&gt; based on your analysis.”

How can i do this directly within MySQL?

答案1

得分: 1

折中效率和效能的方法是将所有变量命名为“variable”,然后使用高于SQL层的工具收集所有变量。尝试为每个变量分配不同的ID可能会导致某种递归查询,这可能会非常低效(尤其是如果你处理成千上万个字符串的情况)。

假设具有相同的变量名称也适用于你,你可以尝试使用REGEXP_REPLACE与反向引用。为了确保匹配的文本不包含其他闭合括号,我们可以使用懒惰操作符?

SELECT REGEXP_REPLACE(txt, 
                      '\\[(.*?)\\]',
                      '<span class="variable">$1</span>') AS txt
FROM tab

如果由于.*?而导致模式匹配看起来太宽松,你可以限制可用的值在括号之间为[A-Za-z ]+?

SELECT REGEXP_REPLACE(txt, 
                      '\\[([A-Za-z ]+?)\\]', 
                      '<span class="variable">$1</span>') AS txt
FROM tab;

或者匹配除了闭合括号之外的任何字符[^\\[]+

SELECT REGEXP_REPLACE(txt, 
                      '\\[([^\\[]+)\\]', 
                      '<span class="variable">$1</span>') AS txt
FROM tab;

输出

txt
如何比较总奖励团队,考虑到行业更好?提供分析的度量标准,包括成本最佳实践。此外,根据你的分析提出任何潜在改进领域的建议。

查看演示链接

英文:

A compromise between efficiency and effectiveness, is assigning the name "variable" to all your variables, and then gather all variables with tools belonging to higher than sql layers. Attempting to assign a different id to each variable could lead you to some kind of recursive query, which would reveal highly inefficient (especially if you're dealing with thousands of strings).

Assuming having the same variable name could work for you as well, you can try using REGEXP_REPLACE with backreference. In order to make sure the matched text does not include other closed parentheses, we can use the lazy operator ?.

SELECT REGEXP_REPLACE(txt, 
                      &#39;\\[(.*?)\\]&#39;,
                      &#39;&lt;span class=&quot;variable&quot;&gt;$1&lt;/span&gt;&#39;) AS txt
FROM tab

If the pattern matching looks too relaxed due to '.*?', you can restrict your available values among brackets [A-Za-z ]+?:

SELECT REGEXP_REPLACE(txt, 
                      &#39;\\[([A-Za-z ]+?)\\]&#39;, 
                      &#39;&lt;span class=&quot;variable&quot;&gt;$1&lt;/span&gt;&#39;) AS txt
FROM tab;

or match any character except closed bracket [^\\[]+:

SELECT REGEXP_REPLACE(txt, 
                      &#39;\\[([^\\[]+)\\]&#39;, 
                      &#39;&lt;span class=&quot;variable&quot;&gt;$1&lt;/span&gt;&#39;) AS txt
FROM tab;

Output:

txt
How can we compare the total rewards &lt;span class="variable">team&lt;/span>, taking into account that &lt;span class="variable">industry&lt;/span> is better? Provide metrics for analysis, including &lt;span class="variable">costs&lt;/span>, and &lt;span class="variable">best practices&lt;/span>. Additionally suggest any &lt;span class="variable">potential areas for improvement&lt;/span> based on your analysis.

Check the demo here.

答案2

得分: 1

假设有一个名为foo的表,其中有一个要更改的列bar和一个主键id:

update foo
join (
    select id, group_concat(case when i then concat('<span class="variable-',i,'">[',replace(substring_index(substring_index(bar,'[',i+1),'[',-1),']','</span>')) else substring_index(bar,'[',1) end order by i separator '') newbar
    from foo
    join (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9 union select 10) i
        on i <= char_length(bar)-char_length(replace(bar,'[',''))
    group by id
) newbar using (id)
set bar=newbar

这将根据左括号的数量拆分每个字符串,并为以括号开头的每个部分添加标记,然后将它们重新连接在一起。

char_length...-char_length(replace... 用于计算字符串中子字符串的数量。

substring_index(substring_index(...,delim,i+1),delim,-1) 用于提取由delim分隔的字符串的第i个部分。

英文:

Assuming a table foo with a column bar to change and an id primary key:

update foo
join (
    select id, group_concat(case when i then concat(&#39;&lt;span class=&quot;variable-&#39;,i,&#39;&quot;&gt;[&#39;,replace(substring_index(substring_index(bar,&#39;[&#39;,i+1),&#39;[&#39;,-1),&#39;]&#39;,&#39;]&lt;/span&gt;&#39;)) else substring_index(bar,&#39;[&#39;,1) end order by i separator &#39;&#39;) newbar
    from foo
    join (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9 union select 10) i
        on i &lt;= char_length(bar)-char_length(replace(bar,&#39;[&#39;,&#39;&#39;))
    group by id
) newbar using (id)
set bar=newbar

fiddle

This splits up each string based on the number of left brackets and adds the spans to each part beginning with a bracket, then rejoins them together.

char_length...-char_length(replace... is how you count the number of substrings in a string.

substring_index(substring_index(...,delim,i+1),delim,-1) is how you extract the ith part of a string delimited by delim.

答案3

得分: 0

我有以下的MySQL SQL查询:

SET @counter := 0;
UPDATE table1 SET prompt = REGEXP_REPLACE(columnA COLLATE utf8mb4_general_ci, '&#39;\\[([^\\]]*)\\]&#39;', CONCAT('&lt;span class=&quot;prompt-variable-&', @counter := @counter + 1, '&quot;&gt;', '\&lt;/span&gt;'));

看起来是一个非常简洁和简单的解决方案。请替换table1columnA。它创建了一个名为counter的变量。使用正则表达式搜索方括号[],然后在周围添加HTMLspan标签。

英文:

I have the following MySQL SQL googled:

SET @counter := 0;
UPDATE table1 SET prompt = REGEXP_REPLACE(columnA COLLATE utf8mb4_general_ci, &#39;\\[([^\\]]*)\\]&#39;, CONCAT(&#39;&lt;span class=&quot;prompt-variable-&#39;, @counter := @counter + 1, &#39;&quot;&gt;&#39;, &#39;\&lt;/span&gt;&#39;));

Seems to be a very slim and simple solution. Replace table1 and columnA. It creates a variable named counter. With Regex it searched for brackets [] and add the html span around it.

huangapple
  • 本文由 发表于 2023年5月28日 16:28:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76350580.html
匿名

发表评论

匿名网友

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

确定