简体   繁体   中英

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. 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.

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:

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
         )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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