user364809
user364809

Reputation: 11

Execution of fluent regex

We tried to run the following code : fluentRegex package contains following java files: RegularExpressionBuilder.java RegularExpressionBuilderBase.java InvalidTokenException.java SingleCharMatcher.java

Codes in the above java files are taken from the following given link: http://code.google.com/p/fluent-regex/source/browse/trunk/src/com/googlecode/fluentregex/?r=8

CODE:

import fluentRegex.*;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

 public class CheckRegex
   {

public CheckRegex(){
String name = "Alen";
     RegularExpressionBuilder regEx =regExp().atLeastOne(anyOf().singleChar(',').whitespace());
  Pattern p = Pattern.compile(regEx.ex());
  Matcher expMatcher = p.matcher(name);
  String st=regEx.ex();
  System.out.println(""+st); 
 if (expMatcher.find())
{
  System.out.println("Month is: ");
}
else
{
    System.out.println("else part!");
}    }  
public void main(String[] args)
     {

    new CheckRegex();
}}

This code is giving following error:

C:\Users\Chitrangada\Documents\docs\DSL-Final yr project\code>javac CheckRegex.j
ava
CheckRegex.java:12: cannot find symbol
symbol  : method anyOf()
location: class CheckRegex
         RegularExpressionBuilder regEx = regExp().atLeastOne(anyOf().singleChar
(',').whitespace());
                                                              ^
CheckRegex.java:12: cannot find symbol
symbol  : method regExp()
location: class CheckRegex
         RegularExpressionBuilder regEx = regExp().atLeastOne(anyOf().singleChar
(',').whitespace());
                                          ^

2 errors

Please reply back soon with a possible solution

Regards,

DSL team

Upvotes: 0

Views: 438

Answers (2)

Bart Kiers
Bart Kiers

Reputation: 170158

You can't just call these methods from your class. The method regExp() and anyOf() seem to be static methods from the RegularExpressionBuilder class. So you'd call them like this:

RegularExpressionBuilder regEx = 
  RegularExpressionBuilder.regExp()
    .atLeastOne(RegularExpressionBuilder.anyOf().singleChar(',').whitespace());

So you seem to be creating the pattern [\s,]+, which compared to that long winded code snippet is much clearer, even for someone not familiar with regex, IMO.

You could import all static methods from RegularExpressionBuilder by importing:

import static com.googlecode.fluentregex.RegularExpressionBuilder.*;

and you can then do:

RegularExpressionBuilder regEx = 
    regExp().atLeastOne(anyOf().singleChar(',').whitespace());

And one more thing: your main method should be made static:

public static void main(String[] args) { 
    // ...
}

Upvotes: 1

outis
outis

Reputation: 77400

Java doesn't allow for free functions, only methods. Thus the calls to anyOf() and regExp() are illegal. Looking at the linked source code, RegularExpressionBuilder has static nullary anyOf() and regExp() methods. Perhaps you meant:

RegularExpressionBuilder regEx =
    RegularExpressionBuilder.regExp().atLeastOne(
        RegularExpressionBuilder.anyOf().singleChar(',').whitespace()
    );

There may be other problems with the code; this one is merely what immediately leapt out.

Upvotes: 0

Related Questions