Thursday, March 8, 2007

regex

Well, i'm slowly learning more regex- usually when I need to accomplish a specific task, in this case, renaming a bunch of files in a folder to match a single naming scheme.

I used the following

([a-zA-Z3.]{1,})[.]([s]{1}[0-9]{1,2}[e]{1}[0-9]{1,2}[0-9]{3,4})

which matches a name, separated by dots, (the 3 is for a specific case) until a . followed by xxx or SxxExx (where x is a number)
etc.

The groups are extracted using

foreach (Match m in regex.Matches(inputString)
{
string Group1 = Match.Groups[1].value;
string Group2 = Match.Groups[2].value;
}

Only the group2 is required for the rename operation, but the group1 is used elsewhere in the program

Note the gotcha with the indexing on the groups, it's 0 based but 0 appears to be the entire string vs the actual group-match.

1 comment:

agrath said...

it's worth noting that | is the or operator in a ( ) group match.

so ([a-z]|[A-Z]) is the same as (a-zA-Z])