import java.util.regex.Pattern; class SampleEncodeRegExp{ public static void main(String[] args){ String escapedRegexp = Pattern.quote("a.c"); System.out.println(escapedRegexp); //=> \Qa.c\E System.out.println("a.c".matches(escapedRegexp)); //=> true System.out.println("abc".matches(escapedRegexp)); //=> false } } String escapedRegexp = Regexp.quote("a.c") puts escapedRegexp