1

I have written following program to write the IPV6 validation.

import java.util.regex.*;
import java.io.*;
import java.util.*;

public class demo
{
    //static String pattern = "([\\da-f]+):([\\da-f]+):([\\da-f]+):([\\da-f]+):([\\da-f]+):([\\da-f]+):([\\da-f]+):([\\da-f]+)";
    public static void main(String []args)
    {
        Pattern p=Pattern.compile(pattern);
        while(true)
        {
            Scanner sc = new Scanner(System.in);
            String line = sc.nextLine();
            if(line.equals(-1))
                System.exit(0);
            Matcher m =p.matcher(line);
            if(m.find())
                System.out.println("Found");
            else
                System.out.println("Not Found");
        }
}
}

But when I am running the code, it is printing "Found" for the below string. "1050:10:0:0:15:600:300c:326h"

I don't know why this is happening.(As last octet has 'h' in it, so it should print "NOT FOUND")

tourism
  • 155
  • 12
  • If you want to check if entire string matches regex you are looking for `matches` method. `find` checks if it can *find* any substring described by regex. If you can't change your method from `find` to `matches` then you need to surround your regex with `^` and `$` anchors. – Pshemo Nov 22 '15 at 23:48
  • Does the ipv6 always has the same structure? If it is the case, you could add `{4}` for example instead of `+` for the last group – Yassin Hajaj Nov 22 '15 at 23:48
  • @Pshem thanks for this – tourism Nov 22 '15 at 23:50
  • @Yassin It may contain 0-4 character. – tourism Nov 22 '15 at 23:50

0 Answers0