Using Functions or Methods in Java String.replaceAll () regex

I am trying to convert an RPN equation to a string that conforms to tigcc rules. There, the numbers should have the number of characters in front of them and a label for positive or negative. For "2" it will be "1 2 POSINT_TAG"

My full input to the rpn converter is based on regular expressions, so I wanted to use them again and have a function String.replaceAll(), for example:

string.replaceAll("(\d+)","$1".length+" $1 POSINT_TAG");

But there he just prints: "2 number INT_TAG". I found some classes such as com.stevesoft.pat ( link ).

Is there any other way implemented in normal Sun Java to use (custom) functions in replacing regex rules?

+5
source share
3 answers

No, at least not the way you would do it in C # or Ruby.

The closest thing to write is this loop:

static Pattern pattern = Pattern.compile("\\d+");
String convert(String input) {
    StringBuffer output = new StringBuffer();
    Matcher matcher = pattern.matcher(input);
    while (matcher.find()) {
        String rep =
            String.format("%d %s POSINT_TAG",
                          matcher.group().length(),
                          matcher.group());
        matcher.appendReplacement(output, rep);
    }
    matcher.appendTail(output);
    return output.toString();
}
+13
source

I have an idea about replacing custom strings with Java. Not as simple as the replacement function in JavaScript, but it just works fine. Here is the code:


    package org.eve.util;

    import java.lang.reflect.Method;
    import java.lang.reflect.Modifier;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    public class StringUtils{
        public static String replaceWithFn(CharSequence input,String regexp,Method fn,int group){
            Matcher m=Pattern.compile(regexp).matcher(input);
            StringBuffer sb=new StringBuffer();
            try {
                Object obj=Modifier.toString(fn.getModifiers()).indexOf("static")>-1?
                        fn.getClass():fn.getDeclaringClass().newInstance();
                if(fn.getReturnType()!=String.class){
                    System.out.println("replacement function must return type \"String\".");
                }
                while(m.find()){
                    m.appendReplacement(sb, (String)fn.invoke(obj, m.group(group)));
                }
                m.appendTail(sb);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return sb.toString();
        }
    }

 


    package org.eve.test;
import org.eve.util.StringUtils;

public class ReplaceTest {
    public static void main(String[] args) {
        try {
            StringBuffer input=new StringBuffer("\\u039D\\u03B9\\u03BA\\u03CC\\u03BB\\u03B1\\u03BF\\u03C2 Nicholas \\u5C3C\\u53E4\\u62C9\\u65AF");
            System.out.println("input="+input);
            String result=StringUtils.replaceWithFn(
                input,
                "\\\\u([0-9a-zA-Z]{4})",
                ReplaceTest.class.getMethod("hex2char",String.class),
                1
            );
            System.out.println("output="+result);
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

    public String hex2char(String s){
        //TODO replaceholder
        return String.valueOf((char)Integer.parseInt(s,16));
    }
}

code>

Just for fun.

+3
source

, . Java , , , - . "$1", , # 1.

, :

string.replaceAll("(\\d+)","$1".length+" $1 POSINT_TAG");   

:

string.replaceAll("(\\d+)","2 $1 POSINT_TAG");  

, , finnw, .

+1

All Articles