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