I searched lot, but cant get perfect solution for below question.
I have a following code to get string values from one line of string by making regex and creating pattern object and matching in string with Matcher.
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("product val='321ddfgsagsdf' find way");
list.add("product val='781ertergdfv' find way");
list.add("product val='96puil$hjgh&fgh' find way"); //Here special characters in string
list.add("product val='25errgfgsagsdf' find way");
list.add("product val='562qweddfgsagsdf' find way");
for (String string : list) {
Pattern myPat = Pattern.compile(" val='(.*?)'", 2);
Matcher m = myPat.matcher(string);
if (m.find()) {
System.out.println("Found value: " + m.group(1));
} }
}
Here I am able to get values for ref, inside the single quotes.
But, third value is having "$" and "&" in ref value, which I don't want to process it in pattern.
I know it is possible by checking string, if string contains special chars and avoid it. But, I want to make it using regex, as my app is checking very much bigger strings.
Is there any way to avoid this special characters in java regex?
valand there is novalin the input. There isref. Please make sure you post what you really have, or we'll be unable to help quickly and efficiently. If you want to follow your blacklisting approach tryPattern myPat = Pattern.compile(" ref='([^\\s\\p{P}\\p{S}]+)'");. However, as I thought initially,Pattern myPat = Pattern.compile(" ref='(\\w+)'");should work, too.