• Example Regexes to Match Common Programming Language Constructs

    Regular expressions are very useful to manipulate source code in a text editor or in a regex-based text processing tool. Most programming languages use similar constructs like keywords, comments and strings. But often there are subtle differences that make it tricky to use the correct regex. When picking a regex from the list of examples below, be sure to read the description with each regex to make sure you are picking the correct one.
    Unless otherwise indicated, all examples below assume that the dot does not match newlines and that the caret and dollar do match at embedded line breaks. In many programming languages, this means that single line mode must be off, and multi line mode must be on.
    When used by themselves, these regular expressions may not have the intended result. If a comment appears inside a string, the comment regex will consider the text inside the string as a comment. The string regex will also match strings inside comments. The solution is to use more than one regular expression and to combine those into a simple parser, like in this pseudo-code:
    GlobalStartPosition := 0;
    while GlobalStartPosition < LengthOfText do
      GlobalMatchPosition := LengthOfText;
      MatchedRegEx := NULL;
      foreach RegEx in RegExList do
        RegEx.StartPosition := GlobalStartPosition;
        if RegEx.Match and RegEx.MatchPosition < GlobalMatchPosition then
          MatchedRegEx := RegEx;
          GlobalMatchPosition := RegEx.MatchPosition;
      if MatchedRegEx <> NULL then
        // At this point, MatchedRegEx indicates which regex matched
        // and you can do whatever processing you want depending on
        // which regex actually matched.
      GlobalStartPosition := GlobalMatchPosition;
    If you put a regex matching a comment and a regex matching a string in RegExList, then you can be sure that the comment regex will not match comments inside strings, and vice versa. Inside the loop you can then process the match according to whether it is a comment or a string.
    An alternative solution is to combine regexes: (comment)|(string). The alternation has the same effect as the code snipped above. Iterate over all the matches of this regex. Inside the loop, check which capturing group found the regex match. If group 1 matched, you have a comment. If group two matched, you have a string. Then process the match according to that.
    You can use this technique to build a full parser. Add regular expressions for all lexical elements in the language or file format you want to parse. Inside the loop, keep track of what was matched so that the following matches can be processed according to their context. For example, if curly braces need to be balanced, increment a counter when an opening brace is matched, and decrement it when a closing brace is matched. Raise an error if the counter goes negative at any point or if it is nonzero when the end of the file is reached.
  • You might also like

    No comments:

    Post a Comment

    Good day precious one, We love you more than anything.

Powered by Blogger.