?
Solved

HowTo: Use PCRE with Visual C++?

Posted on 2006-03-20
6
Medium Priority
?
1,148 Views
Last Modified: 2013-12-14
I want to use PCRE with Visual Studio 6/7/8. My understanding is the latest version of PCRE is 6.4 (but might be 6.6?)

PCRE seems mostly intended for Unix/Linux programmers. The developer seems to have little or no interest in providing nmake or VC++ project files (dsw, dsp, sln, vcproj). I've gotten errors trying to compile/link the pcre files with a demo.cpp file.

I would like a very simple program (with project files) that uses PCRE 6.4 (or later) to do the equivalent of:

// demo.cpp
#include "pcre.h"
#include <stdio.h>
void main(void) {
  char   str[] = "search within this string to find whether cat is contained";
  char   subStr[] = "cat";
  char   replaceStr[] = "dog";
  // pcre statement to search str;
  // pcre statement to replace cat with dog
  printf("%s\n", str);
}
0
Comment
Question by:newton-allan
  • 3
5 Comments
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16235090
To search you can use
char* posOfStr = strstr(str, subStr);
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16235152
Modified code

void main(void) {
  char   str[100] = "search within this string to find whether cat is contained";
  char   tempStr[100];
  char   subStr[] = "cat";
  char   replaceStr[] = "dog";

  strcpy(tempStr, str);
  char* c = strstr(str, subStr);
  strcpy(c, replaceStr);
  char* c1 = strstr(tempStr, subStr);
  strcpy(c + strlen(replaceStr), c1 + strlen(subStr));

  printf("%s\n", str);
}
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16235161
>> char   str[100] = "search within this string to find whether cat is contained";
Give a size to this array. Because if the replaceStr is big then you will get segmentation fault.
0
 
LVL 1

Author Comment

by:newton-allan
ID: 16236136
Sorry if my question wasn't clear. I know how to use strstr to find the string. That isn't the point of my question. I want to use the PCRE (Perl Compatible Regular Expressions) library to do match/replace that will be MUCH more complicated. The "cat" to "dog" example was just to illustrate.

PCRE library link:
http://www.pcre.org/

The real match/replace is going to be something like Bible scripture references, which are of the form:
BookStringName chapNum:verseNum
so that something like the C/C++ search string:
char str[500] = "The Ten Commandments start at Exodus 20:3 and the Sermon on the Mount starts at Matthew 5:3 and the largest encoding would be Psalm 119:176.";

find: (PlaceHolder_0|Genesis|Exodus|Leviticus|...|Psalm|...|Malachi|Matthew|Mark|...|Revelation) ([0.9]{1,3}):([0-9]{1,3})
becomes:
"The Ten Commandments start at <ref=2.20.3>Exodus 20:3</ref> and the Sermon on the Mount starts at <ref=40.5.3>Matthew 5:3<ref> and the largest encoding would be <ref=19.119.176>Psalm 119:176</ref>."

Exodus is the 2nd book, so the encoded reference becomes 2.20.3. Matthew is the 40th book, so the encoded reference becomes 40.5.3. Psalm is the 19th book, and so on.

It would have to find the pattern with a ##:## after it .... encountering just Exodus or just Matthew without the ChapNum:VerseNum pattern immediately after it wouldn't be considered a match.

My understanding is that RegEx was made for tasks such as these when strstr isn't adequate. The problem is getting PCRE to build for vc6 or vc7.1. The PCRE developer primarily supports Linux/Unix, and doesn't seem to have much interest in Windows developers.

0
 

Accepted Solution

by:
GranMod earned 0 total points
ID: 16475712
Closed, 250 points refunded.
GranMod
The Experts Exchange
Community Support Moderator of all Ages
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question