Home > RegEx, VB.NET > Regex grouping for automatic coding

Regex grouping for automatic coding

2015/12/17

Sometimes is requested to do ugly and repetitive tasks.
For example there were some .aspx pages where in a DataGrid was written the DataField for monthly columns, but not the HeaderText (i don’t know why..).
Copy & paste, ok only 13 columns: but a lot of .aspx pages.
So i thinked to an substitution using regular expressions.
Tipically we have a string delimited with a double quotation mark, that we can express as

DataField=(["'])(.*?)\1

Or

DataField=(["'])(.*?)(["'])

Both are explainable as: the string begin with a ‘ or an ” , there could be an variable amount of chars or numbers and then there is a final ‘ ( or “)
The trick is in the parenthesis () , that isolates an regex group.
I use an old but valid tool, the Rad Software Regular Expressions Designer (the website is no more available), the basilar syntax is
0regex
If we introduce an group
1regex
We have surrounded with () the [“‘] expression, and there is a group ‘1’ referred to the char “.
The \1 is a regex reference to the first group (if you delete the parenthesis around [“‘] there is an error while evaluating the regex).
By using
2regex
We isolate the text between the ” couple.
Using the regex expression $<number> we can refer to these groups, so in Notepad++ i used
notepad
And for every DataField with the Replace All is created the HeaderText with the same month name.

Advertisements
Categories: RegEx, VB.NET
%d bloggers like this: