Use Pattern.quote(String)
method:
if (st.matches(Pattern.quote(regex_data))) {
System.out.println(" data matched "); // this is not printed when the pattern is read from the file instead of setting it through literals
}
There are some other issues that you should consider resolving:
You're overwriting the value of
regex_data
in thewhile
loop. Did you intend to store all the the regex pattern in alist
?String#split()[0]
will return aString
only. You don't need to invoketoString()
on that.