[Insight-users] Regular Expression Library

Karthik Krishnan Karthik.Krishnan at kitware.com
Tue Apr 4 00:34:03 EDT 2006


On Mon, 2006-04-03 at 16:56 -0500, Matt Kelsey wrote:
> Thanks for the info.  This looks like it's just what I need.  I wonder
> if you could point me to a small example project or tutorial using
> this to get me started.  I'm familar with RegExp syntex, but a bit of
> a newbie when it comes to ITK and Cmake.  Thanks again,

Sure. The following code will match the following:
  
  "Matches: KarthikIsAnAss KarthikIsAJackAss KarthikIsA@#9$Ass
KarthikIsASmartAss KarthiksGotANiceAss"

#include "itksys/RegularExpression.hxx"
#include <vector>
#include <iostream>

int main()
{
  const char *input = "NoOneIsAnAss KarthikIsAnAss KarthikIsAJackAss
KarthikIsA@#9$Ass KarthikIsASmartAss KarthiksGotANiceAss SomethingElse";
  
  std::string regex = "Karthik(.*)Ass";
  
  // Compile the regular expression.
  itksys::RegularExpression re;
  if(!re.compile(regex.c_str()))
    {
    std::string e = "sub-command REGEX, mode MATCHALL failed to compile
regex \""+regex+"\".";
    return 1;
    }
  
  // Scan through the input for all matches.
  std::string output;
  while(re.find(input))
    {
    std::string::size_type l = re.start();
    std::string::size_type r = re.end();
    if(r-l == 0)
      {
      std::string e = "sub-command REGEX, mode MATCHALL regex \""+regex
+"\" matched an empty string.";
      return 1;
      }
    if(output.length() > 0)
      {
      output += ";";
      }
    output += std::string(input+l, r-l);
    input += r;
    }
    
  std::cout << "Matches: " << output << std::endl;
  return EXIT_SUCCESS;
}



-karthik

You should look at Cmake cvs and search for "RegularExpression" for more
examples.
http://www.itk.org/cgi-bin/viewcvs.cgi/Source/cmStringCommand.cxx?annotate=1.19&root=CMake


> 
> Matt
> 
> 
> 
> On 4/3/06, Karthik Krishnan <Karthik.Krishnan at kitware.com> wrote:
> > kwsys provides basic regex match capabilities in C++
> >
> >
> > http://www.itk.org/cgi-bin/viewcvs.cgi/Utilities/kwsys/RegularExpression.hxx.in?rev=1.8&root=Insight&view=markup
> >
> > The regex syntax for the most part is similar to perl. You will find
> > several examples of kwsys usage in CMake, ITK, VTK CVS repositories.
> >
> > Don't expect the same power as perl though.. there is no s/../../
> > substitute, just ~m/../
> >
> > -karthik
> >
> > Matt Kelsey wrote:
> >
> > >A little off topic here, but does anyone have a favorite Regular
> > >Expression Library that can be included along with ITK and VTK easily
> > >using cmake.  If so, I'd appreciate the cmake lines as well.  I'm
> > >using Python to do the trick right now, but things would be a lot
> > >cleaner to have it all integrated in the main file.
> > >
> > >Thanks,
> > >Matt
> > >_______________________________________________
> > >Insight-users mailing list
> > >Insight-users at itk.org
> > >http://www.itk.org/mailman/listinfo/insight-users
> > >
> > >
> > >
> >
> _______________________________________________
> Insight-users mailing list
> Insight-users at itk.org
> http://www.itk.org/mailman/listinfo/insight-users



More information about the Insight-users mailing list