user2825125
user2825125

Reputation: 91

What is wrong with the pattern matching?

This program is supposed to match

(as(fd))_n

and convert it to

\pochhammer{as(fd)}{n}

However, it is not converting the Strings correctly. It is converting

W_n(-a^2;a,b,c,d)=(a+b)_n(a+c)_n(a+d)_n\,,

to

W_n\pochhammer{-a^2;a,b,c,d)=(a+b}{n}\pochhammer{a+c}{n}\pochhammer{a+d}{n}\,,

when it should be converted to

W_n(-a^2;a,b,c,d)=\pochhammer{a+b}{n}\pochhammer{a+c}{n}\pochhammer{a+d}{n}\,,

This is my code:

while(scanner.hasNextLine()) {
    String line = scanner.nextLine();
    Pattern cpochhammer = Pattern.compile("(\\((.*?)\\)_\\{([^}]+)\\})");
    Matcher pochhammer = cpochhammer.matcher(line);
    StringBuffer rplcmntBfr = new StringBuffer();

    while(pochhammer.find())  {
        pochhammer.appendReplacement(rplcmntBfr, "\\\\pochhammer{$2}{$3}");
    }

    pochhammer.appendTail(rplcmntBfr);  
    Pattern npochhammer = Pattern.compile("(\\((.*?)\\)_(.))");
    Matcher ppochhammer = npochhammer.matcher(rplcmntBfr. toString() );
    rplcmntBfr.setLength(0);

    while(ppochhammer.find())  {
        ppochhammer.appendReplacement(rplcmntBfr, "\\\\pochhammer{$2}{$3}");
    }

    ppochhammer.appendTail(rplcmntBfr);
    writer.println(rplcmntBfr);
}

Thanks.

Upvotes: 0

Views: 64

Answers (1)

Pshemo
Pshemo

Reputation: 124225

I may be mistaken but maybe you are looking for something like

String replaced = line.replaceAll("\\(([^)]*)\\)_(\\w+)", "\\\\pochhammer{$1}{$2}");
//                                                ^^^^ 
//                                  You can use \\d or \\d+ instead
//                                  this part. I am not sure what `n` can be

I can try to correct my answer when you describe your question in more detail, like what you are trying to achieve with your first loop? Replacing "(\\((.*?)\\)_\\{([^}]+)\\})" with \\\\pochhammer{$2}{$3} seems pointless here since there are no (xxx)_{n} in your input.

So only problem seems to be with your second regex which is (\\((.*?)\\)_(.)). If you take a closer look at it you don't need your outer brackets because they will just make group 1 group 2, so instead of

    (\\((.*?)\\)_(.)) you can use 
     \\((.*?)\\)_(.)

Next thing is that you are using .*? which means can match any characters so \\((.*?)\\)_ will match any character first ( and last ) which has _ after it like in your case

 W_n(-a^2;a,b,c,d)=(a+b)_n(a+c)_n(a+d)_n\,,
     ^^^^^^^^^^^^^^^^^^
     this part

which in result gives you

 W_n\pochhammer{-a^2;a,b,c,d)=(a+b}{n}\pochhammer{a+c}{n}\pochhammer{a+d}{n}\,,
                ^^^^^^^^^^^^^^^^^^

To solve this problem you can use [^)] instead of . like in my solution at top of my answer. This way you will only match single, not nested set of parenthesis like (xxx) because x in this case can't be ) ([^)] means - every character except )).

Upvotes: 1

Related Questions