Try with:
(?<!\w)\-p(?!\w)
DEMO
Which means:
(?<!\w)
negative lookbehind for any word character (A-Za-z0-9_) so
if it will be preceded by &*%^%^ it will match anyway,
\-p
- -p
(?!\w)
negative lookahead for any word character (A-Za-z0-9_), as
above
Another solution could be also:
(?<=\s)\-p(?=\s)
then there must be space char (' ') before and anfter -p
Implementation in Java with Pattern and Matcher classes:
public class Test {
public static void main(String[] args) {
String sample = "This is my string that has -policy and -p";
Pattern pattern = Pattern.compile("(?<!\\w)\\-p(?!\\w)");
Matcher matcher = pattern.matcher(sample);
matcher.find();
System.out.println(sample.substring(matcher.start(), matcher.end()));
System.out.println(matcher.group(0));
}
}