简体   繁体   English

SQL 服务器中的正则表达式模式搜索

[英]Regex pattern search in SQL Server

I'm new to SQL and SQL Server in particular trying to extract values from a string based on a regex pattern.我是 SQL 和 SQL 服务器的新手,特别是尝试从基于正则表达式模式的字符串中提取值。 I have two patterns which can be boiled down to:我有两种模式可以归结为:

'string(int:int)'
'string(len=int):int+'

I need to extract one of the intigers from the pattern - in case of the first pattern, intigers from the bracket, from the second pattern, one of the integers in the end.我需要从模式中提取一个整数 - 如果是第一个模式,则从括号中提取整数,从第二个模式中提取一个整数。

It's kind of obvious that this is a bit of a noob question, but comming from Python I really strugle with finding the proper tools in SQL Server to do this.很明显,这是一个菜鸟问题,但来自 Python 我真的很难在 SQL 服务器中找到合适的工具来执行此操作。

I would appreciate if someone has a good example of how to do something like this or has a good tutorial recommendation.如果有人有一个很好的例子来说明如何做这样的事情或者有一个很好的教程推荐,我将不胜感激。

Much appreciated!非常感激!

You can use the combination of the substring and charindex as follows:您可以使用substringcharindex的组合,如下所示:

For first pattern you can use:对于第一个模式,您可以使用:

SUBSTRING(your_string,
          CHARINDEX(your_string,'(') + 1,
          CHARINDEX(your_string,':')-CHARINDEX(your_string,'(')-1
         )

For the second pattern you can use:对于第二种模式,您可以使用:

SUBSTRING(your_string,
          CHARINDEX(your_string,':')+1
         )

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

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