For some reason I am unable to get the pattern match for “preference_network_” followed by any anything else as a single string. I want to be able to test if a preference key contains “preference_network_” using the key.matches() method. How can I get this working, I’ve tried a few things with no success. Thanks is advance.
UPDATE
Sorry for not clarifying. All of these solutions I can perform and know about. What I am trying to do is use the “key.matches(String regularExpression)” function where key is a parameter to the onSharedPreferenceChangeListener method. This is what I am having trouble to get working.
And I’m aware I do not have to use this, I can use the startsWith just fine. I was just wondering.
Thanks
solutions without using patterns:
With patterns
If you want to use
matches()you have to writer full pattern:Because matches performs as if your pattern starts with ^ and ends with $, i.e.
Pattern.compile("^something$").matcher(str).find()is the same asPattern.compile("something").matcher(str).matches()