Click here to Skip to main content
14,871,226 members
Home / Discussions / Regular Expressions
   

Regular Expressions

 
AnswerRe: Correct Expression For 'Any Character Including Whitespace' Pin
Bernhard Hiller7-May-17 23:13
MemberBernhard Hiller7-May-17 23:13 
AnswerRe: Correct Expression For 'Any Character Including Whitespace' Pin
Sonhospa8-May-17 0:24
MemberSonhospa8-May-17 0:24 
GeneralRe: Correct Expression For 'Any Character Including Whitespace' Pin
Richard MacCutchan8-May-17 2:25
mveRichard MacCutchan8-May-17 2:25 
QuestionHow to retain characters preceding a match? Pin
Member 1297660431-Jan-17 15:44
MemberMember 1297660431-Jan-17 15:44 
AnswerRe: How to retain characters preceding a match? Pin
Peter_in_278031-Jan-17 19:27
professionalPeter_in_278031-Jan-17 19:27 
GeneralRe: How to retain characters preceding a match? Pin
Member 1297660431-Jan-17 20:50
MemberMember 1297660431-Jan-17 20:50 
QuestionUsing back references to find data type naming standards violations Pin
Member 124482999-Apr-16 7:06
MemberMember 124482999-Apr-16 7:06 
AnswerRe: Using back references to find data type naming standards violations Pin
George Jonsson16-Apr-16 18:36
professionalGeorge Jonsson16-Apr-16 18:36 
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         Integer


The line
Dim L_V_Scalar_Integer_Item As String

will give the result
expected_type   matched_type   mismatched_type
Integer                        String


Example 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
}


modified 18-Apr-16 1:30am.

GeneralRe: Using back references to find data type naming standards violations Pin
Member 1244829917-Apr-16 17:20
MemberMember 1244829917-Apr-16 17:20 
AnswerRe: Using back references to find data type naming standards violations Pin
George Jonsson17-Apr-16 19:13
professionalGeorge Jonsson17-Apr-16 19:13 
QuestionRe: Using back references to find data type naming standards violations Pin
Member 1244829919-Apr-16 6:54
MemberMember 1244829919-Apr-16 6:54 
AnswerRe: Using back references to find data type naming standards violations Pin
George Jonsson19-Apr-16 18:52
professionalGeorge Jonsson19-Apr-16 18:52 
GeneralRe: Using back references to find data type naming standards violations Pin
Member 1244829923-Apr-16 4:25
MemberMember 1244829923-Apr-16 4:25 
Questionnegation of an expression Pin
Vijjuuu.28-Feb-15 7:19
MemberVijjuuu.28-Feb-15 7:19 
AnswerRe: negation of an expression Pin
Richard Deeming2-Mar-15 2:52
mveRichard Deeming2-Mar-15 2:52 
AnswerRe: negation of an expression Pin
bulrush40026-May-16 0:57
Memberbulrush40026-May-16 0:57 
QuestionHelp with regex HTML form validation Part 2 Pin
robwm110-Oct-14 10:18
Memberrobwm110-Oct-14 10:18 
AnswerRe: Help with regex HTML form validation Part 2 Pin
Matt T Heffron10-Oct-14 11:48
professionalMatt T Heffron10-Oct-14 11:48 
GeneralRe: Help with regex HTML form validation Part 2 Pin
robwm110-Oct-14 12:11
Memberrobwm110-Oct-14 12:11 
AnswerRe: Help with regex HTML form validation Part 2 Pin
Matt T Heffron10-Oct-14 12:26
professionalMatt T Heffron10-Oct-14 12:26 
GeneralRe: Help with regex HTML form validation Part 2 Pin
robwm110-Oct-14 12:34
Memberrobwm110-Oct-14 12:34 
AnswerRe: Help with regex HTML form validation Part 2 Pin
Matt T Heffron10-Oct-14 12:43
professionalMatt T Heffron10-Oct-14 12:43 
GeneralRe: Help with regex HTML form validation Part 2 Pin
robwm110-Oct-14 13:06
Memberrobwm110-Oct-14 13:06 
AnswerRe: Help with regex HTML form validation Part 2 Pin
Matt T Heffron10-Oct-14 13:13
professionalMatt T Heffron10-Oct-14 13:13 
GeneralRe: Help with regex HTML form validation Part 2 Pin
robwm110-Oct-14 13:14
Memberrobwm110-Oct-14 13:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.