0

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());
2
  • 4
    You have the ^ and $ characters - indicating start of string and end of string respectively - in the middle of your pattern. That's never going to work. What are you actually trying to do with the IP address here? Commented Jan 7, 2013 at 14:55
  • 1
    how could it start with ] RESPONSE... and start also with 192\\.? Remove the ^ and $ Commented Jan 7, 2013 at 14:55

1 Answer 1

3

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 ;-)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.