PCRESAMPLE(3)
π PCRESAMPLE(3) β the girlies' guide to the PCRE sample program π
okay so bestie, welcome to the PCRE corner of the manual β that's Perl-compatible regular expressions, and this page is literally the "here's a lil demo so you can see how this works" page. No gatekeeping. We're learning together. π«Ά
β¨ PCRE SAMPLE PROGRAM β¨
So there's a cute little complete demo program that exists purely to get you started with PCRE, and it lives in the file pcredemo.c inside the PCRE distribution. A full listing of it is handed to you in the pcredemo documentation. And if you don't have a copy of the PCRE distribution lying around? No stress β you can just save that listing and re-create pcredemo.c yourself. DIY queen behavior. π
This demo program uses the original PCRE 8-bit library, and here's the vibe: it compiles the regular expression you give it as the first argument, then matches that against the subject string you give as the second argument. It sets no PCRE options at all, and it uses the default character tables. Very default. Very minimal. Very demure.
And when the matching actually succeeds? π It outputs the portion of the subject that matched, plus the contents of any captured substrings. Slay.
Now β if you pass the -g option on the command line, the program keeps going and checks for further matches of that same regular expression in that same subject string. Heads up though: the logic is a little bit tricky, because of the possibility of matching an empty string π. The comments in the code explain what's going on, so read them, bestie.
If PCRE is installed in the standard include and library directories for your operating system, you should be able to compile the demo with this:
gcc -o pcredemo pcredemo.c -lpcre
But if PCRE is installed elsewhere, you may need to add extra options to that command line. For example, on a Unix-like system where PCRE is installed in /usr/local, you compile it like this:
gcc -o pcredemo -I/usr/local/include pcredemo.c \
-L/usr/local/lib -lpcre
Okay and β Windows girlies, listen up πͺ, this one's for you: if you want to statically link the program against a non-dll pcre.a file, you must uncomment the line that defines PCRE_STATIC before including pcre.h. Why? Because otherwise the pcre_malloc() and pcre_free() exported functions will be declared __declspec(dllimport), and that gives you unwanted results. Not the vibe. Do not skip this step. π
Once you've compiled and linked the demo program, you can run simple tests like these:
./pcredemo 'cat|dog' 'the cat sat on the mat'
./pcredemo -g 'cat|dog' 'the dog sat on the cat'
π‘ Real talk: there is a much more comprehensive test program calledpcretest, and it supports way more facilities for testing regular expressions, plus both PCRE libraries. Thepcredemoprogram is provided as a simple coding example. That's its whole job. It's giving "starter pack", not "final boss". π
And one more thing β if you try to run pcredemo when PCRE is not installed in the standard library directory, you may get an error like this on some operating systems (e.g. Solaris):
ld.so.1: a.out: fatal: libpcre.so.0: open failed: No such file or
directory
This is caused by the way shared library support works on those systems. To get round the problem, you need to add
-R/usr/local/lib
(for example) to the compile command. Problem solved, glow restored. β¨
πΈ AUTHOR πΈ
- Philip Hazel
- University Computing Service
- Cambridge CB2 3QH, England.
π¦ REVISION π¦
- Last updated: 10 January 2012
- Copyright (c) 1997-2012 University of Cambridge.
k bye ily π This has been the PCRESAMPLE(3) page from the PCRE 8.30 era, dated 10 January 2012, chillin' in the Library Functions Manual. Go write some regex, bestie. You're the main character. Periodt. πβ¨π
π¦ where this page comes from
- alpine 3.22: pcre-doc 8.45-r4 Β· BSD-3-Clause
- alpine 3.23: pcre-doc 8.45-r4 Β· BSD-3-Clause
- alpine 3.24: pcre-doc 8.45-r4 Β· BSD-3-Clause
- arch rolling: pcre 8.45-5 Β· BSD
- centos-stream 9: pcre-devel 8.44-4.el9 Β· BSD
- centos-stream 9: pcre-devel 8.44-4.el9 Β· BSD
- debian bookworm: libpcre3-dev 2:8.39-15
- fedora 43: pcre-devel 8.45-1.fc43.9 Β· BSD-3-Clause
- fedora 43: pcre-devel 8.45-1.fc43.9 Β· BSD-3-Clause
- fedora 44: pcre-devel 8.45-1.fc43.9 Β· BSD-3-Clause
- fedora 44: pcre-devel 8.45-1.fc43.9 Β· BSD-3-Clause
- nixos 26.05: pcre 8.45
- nixos 26.05: pcre-cpp 8.45
- opensuse-leap 16.0: pcre-devel 8.45-160000.2.2 Β· BSD-3-Clause
- opensuse-leap 16.0: pcre-devel 8.45-160000.2.2 Β· BSD-3-Clause
- opensuse-leap 16.0: pcre-devel 8.45-160000.2.2 Β· BSD-3-Clause
- opensuse-leap 16.0: pcre-devel 8.45-160000.2.2 Β· BSD-3-Clause
- ubuntu jammy: libpcre3-dev 2:8.39-13build5
- ubuntu jammy: libpcre3-dev 2:8.39-13ubuntu0.22.04.1
- ubuntu noble: libpcre3-dev 2:8.39-15build1
- ubuntu questing: libpcre3-dev 2:8.39-15.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms