Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Regular Expressions
  4. Using back references to find data type naming standards violations

Using back references to find data type naming standards violations

Scheduled Pinned Locked Moved Regular Expressions
csharpvisual-studioregexquestion
7 Posts 2 Posters 11 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 12414772
    wrote on last edited by
    #1

    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

    G 1 Reply Last reply
    0
    • U User 12414772

      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

      G Offline
      G Offline
      George Jonsson
      wrote on last edited by
      #2

      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
      }

      U 1 Reply Last reply
      0
      • G George Jonsson

        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
        }

        U Offline
        U Offline
        User 12414772
        wrote on last edited by
        #3

        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

        G 1 Reply Last reply
        0
        • U User 12414772

          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

          G Offline
          G Offline
          George Jonsson
          wrote on last edited by
          #4

          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[^]

          U 1 Reply Last reply
          0
          • G George Jonsson

            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[^]

            U Offline
            U Offline
            User 12414772
            wrote on last edited by
            #5

            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

            G 1 Reply Last reply
            0
            • U User 12414772

              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

              G Offline
              G Offline
              George Jonsson
              wrote on last edited by
              #6

              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

              U 1 Reply Last reply
              0
              • G George Jonsson

                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

                U Offline
                U Offline
                User 12414772
                wrote on last edited by
                #7

                Thank you George. Yes it worked.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups