Nikhil
Nikhil

Reputation: 2883

Regex pattern matching yields false

I have executed the code below, but its result is false. Is my pattern is correct? What is wrong here? If I am wrong please correct me because I am stuck on this.

String name = "] RESPONSE GET - 192.168.200.121 -";
string pat = "] RESPONSE (GET|GETNEXT|GETBULK|SET|TRAP) - ^192\\.168\\.200\\.121$ -";
Pattern p = Pattern.compile(pat);
Matcher m = p.matcher(name);
System.out.println(m.find());

Upvotes: 0

Views: 79

Answers (1)

Cerbrus
Cerbrus

Reputation: 72857

This works:

] RESPONSE (GET|GETNEXT|GETBULK|SET|TRAP) - 192\\.168\\.200\\.121 -

You had the ^ and $ characters in the middle of your string. Those represent the start and end of the string to match, respectively. The start / end of a string can't be in the middle of a string, obviously ;-)

Upvotes: 3

Related Questions