Regex code how to filter all names that contain on

2019-09-01 01:04发布

问题:

How to filter all names that consist of numbers and end with .jpg and/or _number.jpg?

Background info: In SSIS 2008 I have a foreach loop that will store the filename into a variable for all jpg files. The enumorator configuration for Files is currently: *.jpg This will handle all jpg files.

What is the code so it will only handle names likes?:

3417761506233.jpg
3417761506233_1.jpg
5414233177487.jpg
5414233177487_1.jpg
5414233177487_14.jpg

but not names like:

abc.jpg
abc123.jpg
def.png
456.png

The numbers represent EAN codes by the way. I thought about this code:

\d|_|.jpg

but SSIS returns an error stating there are no files that meet the criteria eventhough the files(names) are in the folder.

回答1:

You could use a Script Task within the loop to do the regex filtering: http://microsoft-ssis.blogspot.com/2012/04/regex-filter-for-foreach-loop.html

Or you could use a (free) Third Party Enumerator: http://microsoft-ssis.blogspot.com/2012/04/custom-ssis-component-foreach-file.html



回答2:

For that, you can use the following regex:

^\d+(_\d+)?.jpg$

Demo: http://regex101.com/r/qC7oV3



回答3:

^(\d+(?:_\d+)?\.jpg$)

DEMO --> http://regex101.com/r/dM9rJ7

Matches:

3417761506233.jpg
3417761506233_1.jpg
5414233177487.jpg
5414233177487_1.jpg
5414233177487_14.jpg

Excludes:

abc.jpg
abc123.jpg
def.png
456.png