Showing posts with label Regex to validate alpahanumeric or a set of special characters. Show all posts
Showing posts with label Regex to validate alpahanumeric or a set of special characters. Show all posts

Tuesday, October 15, 2013

Regex to validate alpahanumeric or a set of special characters

I have a requirement to ensure that the string has only alphanumeric or a set of characters like + ( ) , ‘ . – =
I tried like

    String regex = "([a-zA-Z0-9]+)|([\\'|\\()|\\+|\\,|\\-|\\.|\\="]+)";    System.out.println("Regex : " + regex);    Pattern pattern = Pattern.compile(regex);    Matcher matcher = pattern.matcher(str);    if (matcher.matches()) {        System.out.println("Match");    } else {        System.out.println("Not Matching");    }

Bt it is not working, Can anyone assist me please

Thanks in advance

It looks like it will not match a whole string containing a mix of alphanumerics & symbols because of the OR in the middle.

e.g. it wont match abcABC()+, yet will match abcABC & will match ()+

Try:

([a-zA-Z0-9\\'\\(\\+\\)\\,\\-\\.\\=]+)

Hope this helps!

Saturday, September 28, 2013

opencv - how to save Mat image in filestorage

I want to save a floating point one-channel image & I don’t want to convert it. So I decided to use filestorage class to save it yet I couldn’t quite obtain how to do it from the documentation. And what I tried didn’t work. Can anybody assist me with this?

// Write:FileStorage fs("img.xml", FileStorage::WRITE);Mat img;fs << img;// Read:FileStorage fs("img.xml", FileStorage::READ);Mat img;fs >> img;

Writing to file

cv::FileStorage storage("test.yml", cv::FileStorage::WRITE);storage << "img" << img;storage.release();

Reading from file

cv::FileStorage storage("test.yml", cv::FileStorage::READ);storage["img"] >> img;storage.release();