dtuct88226 2016-02-05 17:01
浏览 12
已采纳

在两个字符串之间的列中找到特定字符串并不总是存在?

Long story short, let's assume I have a field in a column called 'whyisthisalist' from 'table' which contains:

{example1:"hereistext";example2:"ohlookmoretext";example3:"isthisevenreal"}

How would I extract the text between example1 and example2, given that example2 isn't always there because it could also look like this:

{example1:"hereistext";example7:"ohlookmoretext"}

It should return "hereistext" if everything goes well.

My idea so far:

$sql = "SELECT SUBSTRING(LEFT(whyisthisalist, LOCATE('\";', whyisthisalist) +0), LOCATE('example1:\"', whyisthisalist) +0, 100)
FROM table
WHERE LOCATE('\";', whyisthisalist) > 0
AND LOCATE('example1:\"', whyisthisalist) > 0 ORDER BY id DESC LIMIT 1";

What needs to be done/doesn't work: I need to get the NEXT occuring "; AFTER the previous located string. Is this even possible? Or is there any other workaround? I'd be glad about some help.

  • 写回答

1条回答 默认 最新

  • doucong6884 2016-02-05 17:31
    关注

    The following regex will get you everything from the first colon to the first semi-colon:

    \:([^;]*)
    

    Simplifying your query like this:

    SELECT `whyisthisalist` REGEXP '\:([^;]*)'
    FROM `table`
    

    MySQL REGEX docs
    MySQL Pattern Matching

    You can also use lookahead's and lookbehind's for the regex:

    (?<=example1:)(.+?)(?=\;)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度