Results 1 to 2 of 2
  1. #1
    Rohit429 is offline Member
    Join Date
    Jul 2010
    Posts
    3
    Rep Power
    0

    Default Please check the regex expression !!

    I am trying to calculate the cyclomatic complexity using regex. So far I have come up with this:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    import java.io.*;

    class cyclomaticCalculator
    {

    // uses the java i/o.
    static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));

    protected String txtFileName;
    protected int count = 1; // start count at one then no need to add 1 to count!

    private static BufferedReader reader;
    // Uses the java.io.*;



    public void Cyclomatic() throws IOException
    {

    try {
    // open the file

    System.out.println("------------------------------" );
    System.out.println("CYCLOMATIC COMPLEXITY PROGRAM " );
    System.out.println("------------------------------" );

    //System.out.println("Enter file name to be read: " );


    // Create object to read textfile from keyboard

    txtFileName = "C:\\Documents and Settings\\227951\\workspace\\Decompiler\\test\\And Or.java";
    System.out.println("\n \n");

    // the buffered reader object
    reader = new BufferedReader(new FileReader(txtFileName));








    // Pattern for keywords and the start of comments
    Pattern p1 = Pattern.compile("(/\\*)|(//)|(\".*?\")|(if|for|while|case|switch)");
    Matcher m1 = p1.matcher("");

    // Pattern for the end of multiline comments
    Pattern p2 = Pattern.compile("\\*/");
    Matcher m2 = p2.matcher("");
    boolean inComment = false;

    int lineNum = 0;
    String line = null;
    while ((line = reader.readLine()) != null)
    {
    lineNum++;
    int startAt = 0;
    if (inComment)
    {
    // In multiline comment; see if it ends in this line
    if (m2.reset(line).find())
    {
    inComment = false;
    startAt = m2.end();
    }
    else
    {
    continue;
    }
    }

    m1.reset(line);
    while (m1.find(startAt))
    {
    if (m1.start(1) != -1)
    {
    // Start of multiline comment
    if (m2.reset(line).find(m1.end()))
    {
    // If it ends in this line, we'll keep looking for keywords
    startAt = m2.end();
    }
    else
    {
    // ...otherwise, just set the flag
    inComment = true;
    break;
    }
    }
    else if (m1.start(2) != -1)
    {
    // End-of-line comment
    break;
    }
    else if (m1.start(3) != -1)
    {
    // String literal
    startAt = m1.end();
    }

    else
    {
    // It's a keyword

    count++;

    break;
    }
    }
    }
    System.out.println("Cyclomatic Complexity : "+count);
    }
    catch (IOException e)
    {

    e.printStackTrace();
    }
    }

    public static void main (String[]args)

    {

    // Create object Complex

    cyclomaticCalculator Complex = new cyclomaticCalculator();

    try {
    Complex.Cyclomatic();
    }
    catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }

    }
    However, this does not take into account logical && and ||. I tried to put it in the pattern p1, but its not giving the desired output. Please help me with regex pattern p1. Thanx.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Replies: 5
    Last Post: 07-27-2010, 01:39 PM
  2. JSF expression inside another JSF expression
    By barbarabxl in forum JavaServer Faces (JSF)
    Replies: 2
    Last Post: 05-21-2010, 04:03 PM
  3. Please Help - Regex
    By BeeGee in forum Advanced Java
    Replies: 0
    Last Post: 04-28-2010, 06:28 PM
  4. Help with regex
    By SteroidalPsycho in forum New To Java
    Replies: 2
    Last Post: 03-29-2010, 01:40 AM
  5. Check validation of the Regex
    By itaipee in forum New To Java
    Replies: 4
    Last Post: 05-26-2009, 12:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •