Search code examples
javaregexcharacterregular-language

Change character value RegEx in java


I need to use '+' character instead of '|' character in regular expression in java. So , if i write something like 0+1 regular expresion 0 "or" 1 input should be accepted. '+' sign should references "OR" operation instead of '|'. How can i do this?


Solution

  • So you will need to create your own automata for regex, what is way to complex... or you just need to do a replace into your string before you call the mathes regex method.

    public static void main(String[] args) {
        String regex = "0+1".replaceAll("\\+", "|");
        System.out.println("0".matches(regex));
    }
    

    but i had to say that is a door for a lot of bugs.