1

I am trying to check if a string contains an exact match. For example:
String str = "This is my string that has -policy and -p"

How can I do the following:

if (str.contains("-p")) {  // Exact match to -p not -policy
System.out.println("This is -p not -policy");

}
Moe
  • 1,427
  • 4
  • 34
  • 54
  • possible duplicate of [How to find the exact word using a regex in Java?](http://stackoverflow.com/questions/9464261/how-to-find-the-exact-word-using-a-regex-in-java) – Maciej Lach Aug 14 '15 at 07:02

3 Answers3

3

To differentiate the -p this below solution simply works. If we add /b in the front then the "test-p" kind of word will also be matched.

String source = "This is -p not -policy";
System.out.println("value is " + Pattern.compile(" -p\\b").matcher(source).find());
Nagappan
  • 346
  • 2
  • 8
  • Simply put, Thanks for your answer. I didn't need the space before -p ("-p\\b") worked for me. QQ I thought \b char \b will give me the exact char in the middle, but it doesn't. can you explain it if you don't mind? – Moe Aug 14 '15 at 08:37
  • 1
    \bis\b means it will defintely match in the string " this is -p not -policy ". But we are having metacharacter hypen ('-') at the begining so it is looking for some character before hypen. Suppose in the string "this is t-p not -policy" it will match the word 't-p' – Nagappan Aug 14 '15 at 09:13
  • Thanks for your detailed explanation!! – Moe Aug 14 '15 at 11:03
1

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));
    }
}
m.cekiera
  • 5,365
  • 5
  • 21
  • 35
  • regex works, but I can't get to work with .matches or contains. can you provide the full code? – Moe Aug 14 '15 at 08:08
  • 1
    @Moe I updated answer with code, .matches returns true only if whole string match with regex, contains doesn't work with regex – m.cekiera Aug 14 '15 at 08:12
  • 1
    Thanks so much for your prompt responses, but I have too many lines of syntax to verify the possible outcome of that one line, so i am looking for the shortest syntax. Pattern.compile(" -p\\b").matcher(source).find()); I hope I can select 2 correct answer!! Thanks again!! – Moe Aug 14 '15 at 08:34
  • @Moe no problem, we choose most useful answer, that all. Just to your knowledge, my answer could be shortened to `Pattern.compile("(?<!\\w)\\-p(?!\\w)").matcher(sample).find()`, it also use Pattern and Matcher, and both class are worth to learn about. – m.cekiera Aug 14 '15 at 09:04
0

You can try this way.

String str = "This is my string that has -policy and -p";
for(String i:str.split(" ")){
   if(i.equals("-p")){ // now you are checking the exact match
     System.out.println("This is -p not -policy");
   }
}
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
  • Thanks, but I am looking for the shortest way to check if the string has what I am looking for. thanks again!! – Moe Aug 14 '15 at 08:38