1<html> 2<head> 3<title>pcre2sample specification</title> 4</head> 5<body bgcolor="#FFFFFF" text="#00005A" link="#0066FF" alink="#3399FF" vlink="#2222BB"> 6<h1>pcre2sample man page</h1> 7<p> 8Return to the <a href="index.html">PCRE2 index page</a>. 9</p> 10<p> 11This page is part of the PCRE2 HTML documentation. It was generated 12automatically from the original man page. If there is any nonsense in it, 13please consult the man page, in case the conversion went wrong. 14<br> 15<br><b> 16PCRE2 SAMPLE PROGRAM 17</b><br> 18<P> 19A simple, complete demonstration program to get you started with using PCRE2 is 20supplied in the file <i>pcre2demo.c</i> in the <b>src</b> directory in the PCRE2 21distribution. A listing of this program is given in the 22<a href="pcre2demo.html"><b>pcre2demo</b></a> 23documentation. If you do not have a copy of the PCRE2 distribution, you can 24save this listing to re-create the contents of <i>pcre2demo.c</i>. 25</P> 26<P> 27The demonstration program compiles the regular expression that is its 28first argument, and matches it against the subject string in its second 29argument. No PCRE2 options are set, and default character tables are used. If 30matching succeeds, the program outputs the portion of the subject that matched, 31together with the contents of any captured substrings. 32</P> 33<P> 34If the -g option is given on the command line, the program then goes on to 35check for further matches of the same regular expression in the same subject 36string. The logic is a little bit tricky because of the possibility of matching 37an empty string. Comments in the code explain what is going on. 38</P> 39<P> 40The code in <b>pcre2demo.c</b> is an 8-bit program that uses the PCRE2 8-bit 41library. It handles strings and characters that are stored in 8-bit code units. 42By default, one character corresponds to one code unit, but if the pattern 43starts with "(*UTF)", both it and the subject are treated as UTF-8 strings, 44where characters may occupy multiple code units. 45</P> 46<P> 47If PCRE2 is installed in the standard include and library directories for your 48operating system, you should be able to compile the demonstration program using 49a command like this: 50<pre> 51 cc -o pcre2demo pcre2demo.c -lpcre2-8 52</pre> 53If PCRE2 is installed elsewhere, you may need to add additional options to the 54command line. For example, on a Unix-like system that has PCRE2 installed in 55<i>/usr/local</i>, you can compile the demonstration program using a command 56like this: 57<pre> 58 cc -o pcre2demo -I/usr/local/include pcre2demo.c -L/usr/local/lib -lpcre2-8 59</pre> 60Once you have built the demonstration program, you can run simple tests like 61this: 62<pre> 63 ./pcre2demo 'cat|dog' 'the cat sat on the mat' 64 ./pcre2demo -g 'cat|dog' 'the dog sat on the cat' 65</pre> 66Note that there is a much more comprehensive test program, called 67<a href="pcre2test.html"><b>pcre2test</b>,</a> 68which supports many more facilities for testing regular expressions using all 69three PCRE2 libraries (8-bit, 16-bit, and 32-bit, though not all three need be 70installed). The 71<a href="pcre2demo.html"><b>pcre2demo</b></a> 72program is provided as a relatively simple coding example. 73</P> 74<P> 75If you try to run 76<a href="pcre2demo.html"><b>pcre2demo</b></a> 77when PCRE2 is not installed in the standard library directory, you may get an 78error like this on some operating systems (e.g. Solaris): 79<pre> 80 ld.so.1: pcre2demo: fatal: libpcre2-8.so.0: open failed: No such file or directory 81</pre> 82This is caused by the way shared library support works on those systems. You 83need to add 84<pre> 85 -R/usr/local/lib 86</pre> 87(for example) to the compile command to get round this problem. 88</P> 89<br><b> 90AUTHOR 91</b><br> 92<P> 93Philip Hazel 94<br> 95University Computing Service 96<br> 97Cambridge, England. 98<br> 99</P> 100<br><b> 101REVISION 102</b><br> 103<P> 104Last updated: 02 February 2016 105<br> 106Copyright © 1997-2016 University of Cambridge. 107<br> 108<p> 109Return to the <a href="index.html">PCRE2 index page</a>. 110</p> 111