in the text you need to change all LVARCHAR (n) to VARCHAR (n) where n is any character
the problem is that I don’t know how to leave "n" in its place
so that LVARCHAR (5) changes to VARCHAR (5)
pattern=Pattern.compile("LVARCHAR(n)").matcher("меняем LVARCHAR(21) и хочу поменять LVARCHAR(14)").replaceAll("VARCHAR(n)");
What should I write in a regular expression so that the number remains in place?

    1 answer 1

    Use backlinks that return the value of exciting submasks (groups):

     String s = "меняем LVARCHAR(21), LVARCHAR(1234)"; String result = s.replaceAll("\\bLVARCHAR\\((\\d+)\\)", "VARCHAR($1)"); System.out.println(result); 

    Java demo

    Details :

    • \\b - word boundary
    • LVARCHAR\\( - text LVARCHAR(
    • (\\d+) - (group number 1) one or more digits
    • \\) - sign )

    The replacement pattern uses $1 , the return number stored in the buffer of the first (and only in this expression) exciting subplot (group).

    • Is it possible to be case insensitive? - DiSayThis
    • Use Pattern.CASE_INSENSITIVE or inline variant (?i) : String result = s.replaceAll("(?i)\\bLVARCHAR\\((\\d+)\\)", "VARCHAR($1)"); - Wiktor Stribiżew