更新 MySQL(使用正则表达式?)
我想将看起来像“someSubdomainInfo.mydomain.com”的值更新为“mydomain.com”(.com 只是一个例子,我想捕获其他所有内容 - 基本上清理子域信息)
考虑使用 ^ ([az].*.)?([a-zA-Z0-9_-].[az]) as RegExp(缺少以数字开头的域,可能是其他域)
有关如何进行的任何想法要写相关的更新语句吗?
谢谢,
编辑:再次查看我的问题,似乎我没有清楚地表达它。 我希望对所有域执行此操作,而不是在一对一的域上。 换句话说,我提前不知道“mydomain.com”是什么。
如果我导出到 csv,我可以使用这个正则表达式并用 \2 替换(在 EditPro 文本编辑器上验证),并且我正在寻找类似于直接在 MySQL 中成为 dome 的东西。
I would like to update values which look like "someSubdomainInfo.mydomain.com" to "mydomain.com" (the .com is just an example, I'd like to catch everything else - basically clean the subdomain info)
Thought of using ^([a-z].*.)?([a-zA-Z0-9_-].[a-z]) as RegExp (missing domain that start with a digit, possibly others)
Any ideas on how to write the relevant update statement?
Thanks,
Edit: looking at my question again, seems I did not present it clearly.
I want this done to all domains, not on a one-by-one domain.
In other words, I don't know what "mydomain.com" is ahead of time.
If I export to a csv I can use this regex and substitute with \2 (verified on EditPro text editor), and I'm looking for something similar to be dome directly in MySQL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SUBSTRING_INDEX() 最适合提取 URL 的部分。
如果您知道域名扩展名,您就可以变得聪明并防止意外更新。否则,您可以在子句中使用 regex ,或者计算
.
的数量,以防止此更新更改您不想清除其子域的 URL。SUBSTRING_INDEX() is best suited for extracting portions of URLs.
If you know the domain extension, you can be clever and prevent accidental updates. Otherwise you could use a regex in the clause, or count the number of
.
s to prevent this update from altering the URLs you don't want to clean of their subdomain.RegExp 在 MySQL 中仍然是原始的。它们适合匹配和比较,但不适合替代。
我的建议是使用字符串函数。
这将在列值中查找根域。如果找到它,它将仅获取字符串的这一部分。否则,它将保留原始字符串。使用上面的
SELECT
语句对其进行测试,并在针对您的架构进行调整后将其设为UPDATE
。RegExp are still primative in MySQL. They are good for matching and comparison, but not so much replacement.
My suggestion would be to use String Functions.
This will look for the root domain in the column value. If it finds it, it will take just this portion of the string. Otherwise, it will keep the original string. Test it with the
SELECT
statement above and make it anUPDATE
once you've adjust it for your schema.