0

I'm trying to use a Scanner to read in lines of code from a string of the form "p.addPoint(x,y);"

The regex format I'm after is:

*anything*.addPoint(*spaces or nothing* OR ,*spaces or nothing*

What I've tried so far isn't working: [[.]+\\.addPoint(&&[\\s]*[,[\\s]*]]

Any ideas what I'm doing wrong?

4
  • 1
    [...] defines a character class, and what you have so far is totally broken. Research the meaning of brackets in regex and try again. Commented Jun 4, 2012 at 0:20
  • I'm confused about the spaces or nothing. Only spaces and nothing else? Aren't you trying to catch numeric values? Commented Jun 4, 2012 at 0:20
  • This is the delimiter regex, basically what passes as a separator between the integers. Commented Jun 4, 2012 at 0:24
  • [.+\\.addPoint(\\s*[,\\s*]] doesn't work either. Commented Jun 4, 2012 at 0:28

2 Answers 2

2

I tested this in Python, but the regexp should be transferable to Java:

>>> regex = '(\w+\.addPoint\(\s*|\s*,\s*|\s*\)\s*)'
>>> re.split(regex, 'poly.addPoint(3, 7)')
['', 'poly.addPoint(', '3', ', ', '7', ')', '']

Your regexp seems seriously malformed. Even if it wasn't, matching infinitely many repetitions of the . wildcard character at the beginning of the string would probably result in huge swaths of text matching that aren't actually relevant/desired.

Edit: Misunderstood the original spec., current regexp should be correct.

Sign up to request clarification or add additional context in comments.

6 Comments

doesn't seem to be working... my input is "poly.addPoint(3, 7); //#5" Note, it's supposed to receive both the 3 and the 7.
What output are you getting for that input, and what output do you desire? More specifically, are you trying to split the string at the match? Are you trying to group the numerical arguments and extract them? More details please.
Using the regex as the Scanner delimiter, I'm trying to use while(hasNextInt()) and grab both the 3 and 7 as integers using nextInt(); twice. Does that make sense? I'm not getting any output from your regex.
I'm not familiar with that particular class, but take a look at whether the current version of the regexp seems to do what you need.
what does the | mean in this case?
|
0

Another way:

public class MyPattern {

    private static final Pattern ADD_POINT;
    static {
        String varName = "[\\p{Alnum}_]++";
        String argVal = "([\\p{Alnum}_\\p{Space}]++)";
        String regex = "(" + varName + ")\\.addPoint\\(" + 
                argVal + "," + 
                argVal + "\\);";
        ADD_POINT = Pattern.compile(regex);
        System.out.println("The Pattern is: " + ADD_POINT.pattern());
    }

    public void findIt(String filename) throws FileNotFoundException {
        Scanner s = new Scanner(new FileReader(filename));

        while (s.findWithinHorizon(ADD_POINT, 0) != null) {
            final MatchResult m = s.match();
            System.out.println(m.group(0));
            System.out.println("   arg1=" + m.group(2).trim());
            System.out.println("   arg2=" + m.group(3).trim());
        }
    }

    public static void main(String[] args) throws FileNotFoundException {
        MyPattern p = new MyPattern();
        final String fname = "addPoint.txt";
        p.findIt(fname);
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.