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