| <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="Asciidoctor 2.0.26"><meta name="description" content="Discussion of the pcre2demo program"><title>pcre2sample(3)</title><link rel="stylesheet" href="./css/document.css"><link rel="stylesheet" href="./css/standalone.css"><meta name="color-scheme" content="light dark"><meta name="theme-color" media="(prefers-color-scheme: light)" content="#0085cc"><meta name="theme-color" media="(prefers-color-scheme: dark)" content="#33b8ff"></head><body class="manpage offline-manual"><header class="offline-nav"><nav><a class="offline-index" href="./index.html">Index</a><a class="offline-home" href="https://www.pcre2.org/"><span aria-hidden="true">🌐︎</span> PCRE2 homepage</a></nav></header><main class="document-page"><header class="document-title"><h1>pcre2sample(3)</h1><p class="document-meta">PCRE2 10.50-DEV manual; updated <time datetime="2025-02-28">28 Feb 2025</time></p></header><div class="document-layout"><article class="prose" id="content"><section class="doc-section level-1"><h2 id="_pcre2_sample_program">PCRE2 sample program</h2><p>A simple, complete demonstration program to get you started with using PCRE2 is | |
| supplied in the file <code>pcre2demo.c</code> in the <code>src</code> directory in the PCRE2 | |
| distribution. A listing of this program is given in the | |
| <a href="./pcre2demo.html"><code>pcre2demo</code></a> | |
| documentation. If you do not have a copy of the PCRE2 distribution, you can | |
| save this listing to recreate the contents of <code>pcre2demo.c</code>.</p> | |
| <p>The demonstration program compiles the regular expression that is its | |
| first argument, and matches it against the subject string in its second | |
| argument. No PCRE2 options are set, and default character tables are used. If | |
| matching succeeds, the program outputs the portion of the subject that matched, | |
| together with the contents of any captured substrings.</p> | |
| <p>If the <code>-g</code> option is given on the command line, the program then goes on to | |
| check for further matches of the same regular expression in the same subject | |
| string. The logic is a little bit tricky because of the possibility of matching | |
| an empty string. Comments in the code explain what is going on.</p> | |
| <p>The code in <code>pcre2demo.c</code> is an 8-bit program that uses the PCRE2 8-bit | |
| library. It handles strings and characters that are stored in 8-bit code units. | |
| By default, one character corresponds to one code unit, but if the pattern | |
| starts with <code class="regex rouge"><span class="pcre2-semantic">(*UTF)</span></code>, both it and the subject are treated as UTF-8 strings, | |
| where characters may occupy multiple code units.</p> | |
| <p>If PCRE2 is installed in the standard include and library directories for your | |
| operating system, you should be able to compile the demonstration program using | |
| a command like this:</p> | |
| <div class="listing-block"><pre class="rouge highlight"><code data-lang="shell">cc <span class="nt">-o</span> pcre2demo pcre2demo.c <span class="nt">-lpcre2-8</span></code></pre></div> | |
| <p>If PCRE2 is installed elsewhere, you may need to add additional options to the | |
| command line. For example, on a Unix-like system that has PCRE2 installed in | |
| <code>/usr/local</code>, you can compile the demonstration program using a command | |
| like this:</p> | |
| <div class="listing-block"><pre class="rouge highlight"><code data-lang="shell">cc <span class="nt">-o</span> pcre2demo <span class="nt">-I</span>/usr/local/include pcre2demo.c <span class="se">\</span> | |
| <span class="nt">-L</span>/usr/local/lib <span class="nt">-lpcre2-8</span></code></pre></div> | |
| <p>Once you have built the demonstration program, you can run simple tests like | |
| this:</p> | |
| <div class="listing-block"><pre class="rouge highlight"><code data-lang="shell">./pcre2demo <span class="s1">'cat|dog'</span> <span class="s1">'the cat sat on the mat'</span> | |
| ./pcre2demo <span class="nt">-g</span> <span class="s1">'cat|dog'</span> <span class="s1">'the dog sat on the cat'</span> | |
| ./pcre2demo <span class="nt">-i</span> <span class="s1">'cat'</span> <span class="s1">'the dog sat on the CAT'</span></code></pre></div> | |
| <p>Note that there is a much more comprehensive test program, called | |
| <a href="./pcre2test.html"><code>pcre2test</code>,</a> | |
| which supports many more facilities for testing regular expressions using all | |
| three PCRE2 libraries (8-bit, 16-bit, and 32-bit, though not all three need be | |
| installed). The | |
| <a href="./pcre2demo.html"><code>pcre2demo</code></a> | |
| program is provided as a relatively simple coding example.</p> | |
| <p>If you try to run | |
| <a href="./pcre2demo.html"><code>pcre2demo</code></a> | |
| when PCRE2 is not installed in the standard library directory, you may get an | |
| error like this on some operating systems (e.g. Solaris):</p> | |
| <div class="literal-block"><pre>ld.so.1: pcre2demo: fatal: libpcre2-8.so.0: open failed: No such | |
| file or directory</pre></div> | |
| <p>This is caused by the way shared library support works on those systems. You | |
| need to add</p> | |
| <div class="listing-block"><pre class="rouge highlight"><code data-lang="shell"><span class="nt">-R</span>/usr/local/lib</code></pre></div> | |
| <p>(for example) to the compile command to get round this problem.</p> | |
| <h2 id="_author" class="discrete">Author</h2> | |
| <p>Philip Hazel<br> | |
| Retired from University Computing Service<br> | |
| Cambridge, England.</p> | |
| <h2 id="_revision" class="discrete">Revision</h2> | |
| <p>Last updated: 28 February 2025<br> | |
| Copyright © 1997-2016 University of Cambridge.</p></section></article></div></main><footer class="offline-footer"></footer></body></html> |