Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regular expressions
#1
I’ve come across a nice Windows library which lets you use regular expression matching. It’s called PCRE, and allows for (almost) all the regular expression capabilities available in perl.

You can download it from
http://sourceforge.net/project/showfile ... _id=146854
(You want the pcre library file.)

To use it, #compile the following lines
dll# "pcre.dll" pcre_compile pattern options errptr erroffset tableptr
dll# "pcre.dll" pcre_exec code extra subject length startoffset options ovector ovecsize

And here’s an example script performing a simple check for a match:
“ovector” is an array of match subpattern locations. ovector[0] and [1] give the beginning and end of the whole match. This example searches theText for “[a-z]+g” — a series of letters ([a-z]+) ending in a g. For docs on the flags and such, check the PCRE documentation.


str pattern = "[a-z]+g"
byte* errorMessage = 0
int options = 0
int erroffset = 0

byte* compiledRegEx = pcre_compile(pattern, options, &errorMessage, &erroffset, 0)
if( compiledRegEx = 0 )
,out "it didn't compile"
,if (errorMessage != 0)
,,out "%s at %i" errorMessage erroffset
,,free errorMessage
else
,out "it compiled"
,int* ovector = calloc(30 sizeof(int))
,int searchResult
,str theText = "this is the text being searched"
,int startOffset = 0
,int theOptions = 0
,
,searchResult = pcre_exec(compiledRegEx, 0, theText, theText.len, startOffset, theOptions, ovector, 30)
,if( searchResult > 0 )
,,if( (ovector[0] < theText.len) && (ovector[1]-1 < theText.len) && (ovector[1] > 0) )
,,,out "match result was from %i (%c) to %i (%c)" ovector[0] theText[ovector[0]] ovector[1]-1 theText[ovector[1]-1]
,,else
,,,out "match start was at %i; match end was at %i" ovector[0] ovector[1]-1
,else
,,out "it didn’t match"
,free compiledRegEx
,free ovector
9: ) Lindsey Dubb
#2
Just in case anyone wanders across this old thread — Quick Macros now has regular expressions (via PCRE) built-in with a variety of more useful applications than this macro. So use that, instead!
9: ) Lindsey Dubb


Forum Jump:


Users browsing this thread: 1 Guest(s)