简体   繁体   English

使用SQL中的正则表达式更新字符串

[英]Update a string using regular expressions in SQL

I have a string in my certain columns of my database: 我的数据库的某些列中有一个字符串:

<img title="\frac{3}{8}" src="http://latex.codecogs.com/gif.latex?\dpi{50}&amp;space;\fn_phv&amp;space;\frac{3}{8}" alt="" />

Basically it contains a html code for a fraction. 基本上,它包含一个分数的html代码。 But now I would like to replace it with: 但是现在我想用:

<sup>3</sup>&frasl;<sub>8</sub>

I know I can update the value in the database as such: 我知道我可以像这样更新数据库中的值:

UPDATE table 
SET `field` = Replace(`option`, '<img title="\frac{3}{8}" src="http://latex.codecogs.com/gif.latex?\dpi{50}&amp;space;\fn_phv&amp;space;\frac{3}{8}" alt="" />','<sup>3</sup>&frasl;<sub>8</sub>')
WHERE `filed` LIKE  '%<img title="\frac{3}{8}" src="http://latex.codecogs.com/gif.latex?\dpi{50}&amp;space;\fn_phv&amp;space;\frac{3}{8}" alt="" />%'

However, the word "\\frac{3}{8}" can change accordingly. 但是,单词“ \\ frac {3} {8}”可以相应地更改。 The number in the parentheses can change and when it changes I need to change the html tag that will replace to change accordingly as well. 括号中的数字可以更改,当它更改时,我需要更改将替换的html标记以进行相应的更改。 I know I need to use regular expressions, but not sure how to do it in SQL. 我知道我需要使用正则表达式,但不确定如何在SQL中使用它。

Need some guidance to do it. 需要一些指导来做到这一点。

Why update the table? 为什么要更新表格? Why even have this stored in a table? 为什么还要将其存储在表中?

It seems like your application code (PHP?) could construct the string: 看来您的应用程序代码(PHP?)可以构造字符串:

$numerator = 3;
$denominator = 8;
$img = "<img
      title="\frac{$numerator}{$denominator}"
      src=\"http://latex.codecogs.com/gif.latex?\dpi{50}&amp;
            space;\fn_phv&amp;space;\frac{$numerator}{$denominator}\"
      alt=''
      />";

(etc) (等等)

There is no REGEXP_REPLACE , except in MariaDB. 除了MariaDB外,没有REGEXP_REPLACE

In MySQL, regular expressions are only supported for finding "matches", the REGEXP operator, which returns a boolean. 在MySQL中,仅支持使用正则表达式来查找“匹配项”,即REGEXP运算符,该运算符返回布尔值。 There is no support for using regular expressions to return another value. 不支持使用正则表达式返回另一个值。

Reference: 12.5.2 Regular Expressions https://dev.mysql.com/doc/refman/5.6/en/regexp.html 参考: 12.5.2正则表达式 https://dev.mysql.com/doc/refman/5.6/en/regexp.html


You said that you " need to use regular expressions " to do string manipulation. 您说过,“ 需要使用正则表达式 ”来进行字符串操作。 That means you will have to extract the column values from the database into a client application, and perform the string manipulation in the client app. 这意味着您将必须将数据库中的列值提取到客户端应用程序中,并在客户端应用程序中执行字符串操作。 (From the client, issue an UPDATE statement to assign a new value to the column, replacing the existing value.) (从客户端发出UPDATE语句,以将新值分配给该列,以替换现有值。)


EDIT 编辑

If you absolutely had to do this in a SQL statement, then one option is to create a user defined function that could be used to perform the string manipulation. 如果您绝对必须在SQL语句中执行此操作,那么一种选择是创建用户定义的函数,该函数可用于执行字符串操作。

https://dev.mysql.com/doc/refman/5.6/en/adding-functions.html https://dev.mysql.com/doc/refman/5.6/zh-CN/adding-functions.html

It's possible someone has already coded one that does something similar to what you need. 可能有人已经编码了某个代码,该代码的功能与您所需的功能相似。 I don't want to give the impression that user-defined functions are a silver bullet; 我不想给人以用户定义函数是灵丹妙药的印象。 I mention it because it is an option. 我提到它是因为它是一种选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM