• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 919
  • Last Modified:

How to validate the given boost regular expression

Hi All,

Im doing filtering the files/folders path. i use boost library to filter the given files/folders path. im using the Boost::Regex_Search() function for this. My question is how to validate the given regular expression is valid one. for example '^' is valid one. but if somebody giving the wrong one like "))))((()(()()". i need to validate this and give a alert to user that you are giving the wrog regular expression. i searched in Boost documentation and found regex_error_types. but im bit of confusing in that. is it possible to validate regular expressions using PHP or AJAX or Java script.

Please avoid ereg() functions.

Thanks in Advance...

- Prince
0
ilava
Asked:
ilava
  • 4
2 Solutions
 
peprCommented:
The regular expression string has to be compiled into the internal form when the boost regular expression object is created. When the syntax is bad, the exception is thrown (class regex_error derived from std::runtime_error). The error_type is to be contained inside the exception object of the regex_error class. See http://beta.boost.org/doc/libs/1_33_1/libs/regex/doc/bad_expression.html for example.

This way, you can get the string, try to create the regex object and catch the exception that is thrown when the syntax is bad. After that try to find the reason (the details) and as the user to correct the syntax.
0
 
peprCommented:
... ask the user...
0
 
peprCommented:
In other languages, the regular expression can possibly be compiled on-the-fly, but the internal structure (a kind of finite automaton) is always built first. In other words, you will always be notified somehow if the syntax is bad.
0
 
peprCommented:
Try this example

========================================================
#include <string>
#include <iostream>
#include <boost/regex.hpp>

using namespace std;

int main()
{
    string e[7] = { "123", "1234", "123$", "^0123.+", "^.{3}34.*", ")bad(", "(good)"};
    string s[2] = { "0123456", "some good string"};
   
    for (int i = 0; i < 2; ++i)
    {
        cout << "\nstring: '" << s[i] << "'\n";
        for (int j = 0; j < 7; ++j)
        {
            try
            {
                boost::regex rex(e[j]);
                cout << "   rex: '" << e[j] << "' -- " 
                     << boost::regex_search(s[i], rex) << "\n";
            }
            catch (const boost::regex_error & err)
            {
                cout << "   rex bad syntax: '" << e[j] << "' -- "
                     << err.what() << "\n";
            }
        }
    }
    return 0;
}
========================================================

You will get the following output:

========================================================

string: '0123456'
   rex: '123' -- 1
   rex: '1234' -- 1
   rex: '123$' -- 0
   rex: '^0123.+' -- 1
   rex: '^.{3}34.*' -- 1
   rex bad syntax: ')bad(' -- Unmatched ( or \(
   rex: '(good)' -- 0

string: 'some good string'
   rex: '123' -- 0
   rex: '1234' -- 0
   rex: '123$' -- 0
   rex: '^0123.+' -- 0
   rex: '^.{3}34.*' -- 0
   rex bad syntax: ')bad(' -- Unmatched ( or \(
   rex: '(good)' -- 1
========================================================


0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now