Help with creating a regex pattern

Hi all,

I am new to RegEx, and it seems very confusing to me. I need to create a regex pattern to loop through a list of strings, and one by one check if the strings match at least one of a list/set of patterns.

I.e. I would for example have a list of patterns:
- Starts with Abc and ends with 123
- Starts with Cde and ends with 234
- Starts with Efg and ends with 345
etc.
(not case-sensitive)

I would have to create a regex expression (built in code, at runtime) that can be used to check each string in a list of strings one at a time to see if it matches pattern 1, pattern 2 or pattern 3 etc

Could anyone please give me a hint (or even better the solution) to this problem?

Peter
pblsoftAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pblsoftAuthor Commented:
Just to clarify: It just needs to check if the string matches any of the patterns
0
Terry WoodsIT GuruCommented:
Which language/tool are you using?

I can help with creating regex patterns, but you might need someone familiar with the language you're using to help with loops etc.
0
pblsoftAuthor Commented:
I am working in VS 2010 /C# and am quite proficient in the language, it is only in creation of the regular expression I am short of knowledge.

Peter
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Terry WoodsIT GuruCommented:
For your examples then -

- Starts with Abc and ends with 123

^Abc.*123$

- Starts with Cde and ends with 234

^Cde.*234$

- Starts with Efg and ends with 345

^Efg.*345$

The ^ matches the start of the string, and the $ matches the end. The .* matches any number of (any) characters, (including none, eg with Efg345).

I find this cheat sheet to be fantastic - the pattern modifiers are a little different for C# but the pattern syntax is otherwise pretty much the same.
0
pblsoftAuthor Commented:
Yes. So far so good. This is the basis. What I need to do is to "or"-combine these to one regular expression to use in one match command like:

Regex regex = new Regex("^Abc.*123$(or)^Cde.*234$(or)^Efg.*345$");

if (regex.IsMatch(myString)
  // do something

My problem is how to define the "(or)" parts, I believe it should be possible

I have seen lots of cheat sheets but haven't found this anywhere

I will be building the actual regex string in code from a list of 1 to n criteria

Peter
0
Terry WoodsIT GuruCommented:
It's refreshing to have such a clear description of what you need! The following syntax does as you describe:

^(Abc.*123|Cde.*234|Efg.*345)$
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pblsoftAuthor Commented:
Hi TerryAtOpus

Thanks al lot for your help, it was excellent. For anybody else to peruse, here is my final solution to use this in an Infragistics UltraGrid as a ColumnFilter:

        public void DoFilter()
        {
            label1.Text = "Working...";
            Application.DoEvents();
            Grid.DisplayLayout.Bands[0].ColumnFilters.ClearAllFilters();

            ColumnFilter cf = Grid.DisplayLayout.Bands[0].ColumnFilters["Key"];
            cf.FilterConditions.Clear();
            cf.LogicalOperator = FilterLogicalOperator.And;

            // _keywordsList is a list of strings to match (contains)
            if (_keywordsList != null)
                foreach (FilterCondition cfc in
                    from keyWord in _keywordsList
                    where keyWord != string.Empty
                    select new FilterCondition(FilterComparisionOperator.Contains, keyWord))
                        cf.FilterConditions.Add(cfc);

            if (FilterByNames != null)
            {
                string regEx = string.Empty;
                // Collect "or" regEx from wildcard patterns
                // Build RegEx like: ^(Abc.*123|Cde.*234|Efg.*345)$
                // Input strings are names of locations with or without wildcard
                // I.e
                // - East*Off to match EastNewOff or EastOldOff (start + end)
                // - West*On to match WestNewOn or WestOldOn (start + end)
                // - AVV to match all that contains AVV
                foreach (var searchLocation in FilterByNames)
                {
                    if (regEx == string.Empty)
                        regEx += "^(";
                    else
                        regEx += "|";
                    string filterName = searchLocation.Value.FilterName;
                    if (!filterName.Contains("*"))
                    {
                        // No wildcard in FilterName. Simple contains filter
                        regEx += ".*" + filterName + ".*";
                    }
                    else
                    {
                        // Split in two and add "startswith" and "endswith"
                        string[] parts = filterName.Split(Convert.ToChar("*"));
                        regEx += parts[0] + ".*" + parts[1];
                    }
                }
                if (regEx != string.Empty)
                {
                    regEx += ")$";
                    FilterCondition cfc = new FilterCondition(FilterComparisionOperator.Match, regEx);
                    cf.FilterConditions.Add(cfc);
                }
            }

            Grid.Rows.FilterRow.ApplyFilters();

            label1.Text = _rowCount == Grid.Rows.FilteredInRowCount
                ? string.Format("{0} items", _rowCount)
                : string.Format("{0} of {1} items (filtered)", Grid.Rows.FilteredInRowCount, _rowCount);
        }

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.