Match IDs preceded by "case" with SSIS script using C # regex

I am using SSIS script component to extract some information from a string.

I would like the id in the string in the template below: case: 123455

It is embedded in a string like below:

This is a string. We want case:12345 and case:5656759 in the title 2.

      

I would like to extract ID 12345 and 565675 from a string. The "case" entry is dynamic, it can be nothing, it can display 1 or more 1 per line. I need help with the regex syntax to retrieve information.

The following cases must be recorded:

case:12345, case :12345, case: 12345, case : 12345, Case:12345, CASE:12345

      

+3


source to share


2 answers


You can use look and feel and \d+

:

(?i)(?<=\bcase:)\d+

      

Watch the demo

This regex will match 1 or more digits that appear after the literal string case:

, where case

is the whole word.

If you want the version with a capture group, here it is:

(?i)\bcase:(\d+)

      

The value you need will be stored in capture group 1.



UPDATE

To allow extra spaces in the pattern, use

(?i)(?<=\bcase\s*:\s*)\d+

      

Or a version without lookbehind:

(?i)\bcase\s*:\s*(\d+)    

      

Create a case-insensitive template

The (?i)

inline option / makes the template case insensitive, it will match case

and case

. If you only want to match case

and case

and case

, you need to use (?:[cC]ase|CASE)

.

+2


source


string regex = @"\s(?i)case\s{0,1}:\s{0,1}(\d+)";  

      



Demo

+2


source







All Articles