samedi 21 juillet 2018

replacing certain characters in string REGEX

I am trying to replace some characters that occur only inside parentheses, here are the patterns in which I need the characters to be replaced: Z should be turned into boolean J should be replaced by short L should be replaced by long I should be replaced by int S should be replaced by string S should be replaced by byte

Here are some examples of how my regular expression should behave:

INPUT: net.sourceforge.ganttproject.shape.PaintCellRenderer.getListCellRendererComponent(Ljavax/swing/JList;Ljava/lang/Object;IZZ)

OUTPUT: net.sourceforge.ganttproject.shape.PaintCellRenderer.getListCellRendererComponent(Ljavax/swing/JList;Ljava/lang/Object;int;boolean;boolean)

INPUT: net.sourceforge.ganttproject.ChartComponentBase$1.-init-(Lnet/sourceforge/ganttproject/ChartComponentBase;)

OUTPUT: net.sourceforge.ganttproject.ChartComponentBase$1.-init-(Lnet/sourceforge/ganttproject/ChartComponentBase;)

The problem that I have is that even the B in ChartComponentBase ends up being replaced and I end up with "ChartComponentbytease"

The pattern should only work if there is a succession of capital letters to be replaced preceded by semi colons Only the capital letters within parentheses should be replaced

I tried parsing the string and separating the part within parentheses and using regex then reassembling the string but it did not work:

                    String regEx = "[A-Z]";
                    Pattern pattern = Pattern.compile(regEx);

                    String methodname=method.substring(0, method.indexOf("(")); 
                    String methodparam=method.substring(method.indexOf("(")+1, method.indexOf(")")); 
                    String[] words = methodparam.split("(?<!^)(?=[ISLBY])");

Aucun commentaire:

Enregistrer un commentaire