Using back references to find data type naming standards violations
-
Hello, Using Visual Studio 2015, the first following line RegEx finds the second following line Dim statement: Dim L_V_Scalar_(Integer)_Item As \1 Dim L_V_Scalar_Integer_Item As Integer Is there a way to write a back reference with a ^ not operator to find the following violations of my naming convention? Dim L_V_Scalar_Integer_Item As String I tried the following regular expressions, but they do not work: Dim L_V_Scalar_(Integer)_Item As ^\1 Dim L_V_Scalar_(Integer)_Item As [^\1] Many thanks. Keith
-
Hello, Using Visual Studio 2015, the first following line RegEx finds the second following line Dim statement: Dim L_V_Scalar_(Integer)_Item As \1 Dim L_V_Scalar_Integer_Item As Integer Is there a way to write a back reference with a ^ not operator to find the following violations of my naming convention? Dim L_V_Scalar_Integer_Item As String I tried the following regular expressions, but they do not work: Dim L_V_Scalar_(Integer)_Item As ^\1 Dim L_V_Scalar_(Integer)_Item As [^\1] Many thanks. Keith
There is most likely a more elegant way to do this, but this expression should work. [EDIT] It is the second group that should be back-referenced, hence \2
Dim\s+(?<variable_name>\w_\w_\w+_(?<expected_type>\w+)_\w+)\s+As\s+(?:(?<matched_type>\2)|(?<mismatched_type>\w+))
The line
Dim L_V_Scalar_Integer_Item As Integer
will give the result
expected_type matched_type mismatched_type
Integer IntegerThe line
Dim L_V_Scalar_Integer_Item As String
will give the result
expected_type matched_type mismatched_type
Integer StringExample in c#
Match m = Regex.Match("Dim L_V_Scalar_Integer_Item As String",
"Dim\s+(?<variable_name>\w_\w_\w+_(?<expected_type>\w+)_\w+)\s+As\s+(?:(?<matched_type>\2)|(?<mismatched_type>\w+))");
if (m.Success)
{
// For example, check if the group named 'matched_type' is empty
// This means that there is a violation of the naming convention
if (String.IsNullOrEmpty(m.Groups["matched_type"].Value))
{
string errorText = String.Format(@"The type '{0}' is not allowed for variable name '{1}'.
The correct type is '{2}'",
m.Groups["mismatched_type"].Value,
m.Groups["variable_name"].Value,
m.Groups["expected_type"].Value);
}
}
else
{
// Do some error handling for a failed match
} -
There is most likely a more elegant way to do this, but this expression should work. [EDIT] It is the second group that should be back-referenced, hence \2
Dim\s+(?<variable_name>\w_\w_\w+_(?<expected_type>\w+)_\w+)\s+As\s+(?:(?<matched_type>\2)|(?<mismatched_type>\w+))
The line
Dim L_V_Scalar_Integer_Item As Integer
will give the result
expected_type matched_type mismatched_type
Integer IntegerThe line
Dim L_V_Scalar_Integer_Item As String
will give the result
expected_type matched_type mismatched_type
Integer StringExample in c#
Match m = Regex.Match("Dim L_V_Scalar_Integer_Item As String",
"Dim\s+(?<variable_name>\w_\w_\w+_(?<expected_type>\w+)_\w+)\s+As\s+(?:(?<matched_type>\2)|(?<mismatched_type>\w+))");
if (m.Success)
{
// For example, check if the group named 'matched_type' is empty
// This means that there is a violation of the naming convention
if (String.IsNullOrEmpty(m.Groups["matched_type"].Value))
{
string errorText = String.Format(@"The type '{0}' is not allowed for variable name '{1}'.
The correct type is '{2}'",
m.Groups["mismatched_type"].Value,
m.Groups["variable_name"].Value,
m.Groups["expected_type"].Value);
}
}
else
{
// Do some error handling for a failed match
}Hello George, That's very helpful. Is there a way to get a result running this interactively in Visual Studio rather than via code? I am basically trying to find non-conformances and fix them manually, one by one. Many thanks. Keith
-
Hello George, That's very helpful. Is there a way to get a result running this interactively in Visual Studio rather than via code? I am basically trying to find non-conformances and fix them manually, one by one. Many thanks. Keith
I suppose you could use the Code Analysis part of Visual Studio and add your own rule set. However, if you can edit the rules depends on the version of VS you have.
Quote:
In Visual Studio Ultimate, Visual Studio Premium, and Visual Studio Professional, you can create and modify a custom rule set to meet specific project needs associated with code analysis
See Using Rule Sets to Group Code Analysis Rules[^] Another way could be to create your own Custom Tool that run through your code and show you the violations in a dialog box or something. This is includes quite a bit of work and some understanding of COM programming. See Implementing Single-File Generators[^] for more information. This Codeproject article might also be helpful: Writing a Single-File Generator[^]
-
I suppose you could use the Code Analysis part of Visual Studio and add your own rule set. However, if you can edit the rules depends on the version of VS you have.
Quote:
In Visual Studio Ultimate, Visual Studio Premium, and Visual Studio Professional, you can create and modify a custom rule set to meet specific project needs associated with code analysis
See Using Rule Sets to Group Code Analysis Rules[^] Another way could be to create your own Custom Tool that run through your code and show you the violations in a dialog box or something. This is includes quite a bit of work and some understanding of COM programming. See Implementing Single-File Generators[^] for more information. This Codeproject article might also be helpful: Writing a Single-File Generator[^]
Thanks for the ideas George. I have managed to achieve the desired result in the Visual Studio Find And Replace dialog, with the following regular expression: _Scalar_+(Boolean|Byte|Char|Date|Decimal|Double|Integer|Long|Object|SByte|Short|Single|String|UInteger|ULong|User\-Defined|UShort)_[A-Za-z0-9]+ As (?!\1)\w+ However, I am still trying to fix the problem that I describe in the following link: http://forums.devshed.com/regex-programming-147/nested-pattern-matching-visual-studio-2015-a-973751.html[^] KR, Keith
-
Thanks for the ideas George. I have managed to achieve the desired result in the Visual Studio Find And Replace dialog, with the following regular expression: _Scalar_+(Boolean|Byte|Char|Date|Decimal|Double|Integer|Long|Object|SByte|Short|Single|String|UInteger|ULong|User\-Defined|UShort)_[A-Za-z0-9]+ As (?!\1)\w+ However, I am still trying to fix the problem that I describe in the following link: http://forums.devshed.com/regex-programming-147/nested-pattern-matching-visual-studio-2015-a-973751.html[^] KR, Keith
I think you got the answer already at that site. The expression, as given in the answer,
Dim L_V_Scalar_([^_\s]+)_([^\s]+) As (?>(\w+\.)*)(?!\1)\w+
should work for you
-
I think you got the answer already at that site. The expression, as given in the answer,
Dim L_V_Scalar_([^_\s]+)_([^\s]+) As (?>(\w+\.)*)(?!\1)\w+
should work for you
Thank you George. Yes it worked.