glib-Glob-style-pattern-matching.html   [plain text]


<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Glob-style pattern matching</title><meta name="generator" content="DocBook XSL Stylesheets V1.65.1"><link rel="home" href="index.html" title="GLib Reference Manual"><link rel="up" href="glib-utilities.html" title="GLib Utilities"><link rel="previous" href="glib-Shell-related-Utilities.html" title="Shell-related Utilities"><link rel="next" href="glib-Simple-XML-Subset-Parser.html" title="Simple XML Subset Parser"><link rel="chapter" href="glib.html" title="GLib Overview"><link rel="refentry" href="glib-building.html" title="Compiling the GLib package"><link rel="refentry" href="glib-cross-compiling.html" title="Cross-compiling the GLib package"><link rel="refentry" href="glib-compiling.html" title="Compiling GLib Applications"><link rel="refentry" href="glib-running.html" title="Running GLib Applications"><link rel="refentry" href="glib-changes.html" title="Changes to GLib"><link rel="refentry" href="glib-resources.html" title="Mailing lists and bug reports"><link rel="chapter" href="glib-fundamentals.html" title="GLib Fundamentals"><link rel="refentry" href="glib-Basic-Types.html" title="Basic Types"><link rel="refentry" href="glib-Limits-of-Basic-Types.html" title="Limits of Basic Types"><link rel="refentry" href="glib-Standard-Macros.html" title="Standard Macros"><link rel="refentry" href="glib-Type-Conversion-Macros.html" title="Type Conversion Macros"><link rel="refentry" href="glib-Byte-Order-Macros.html" title="Byte Order Macros"><link rel="refentry" href="glib-Numerical-Definitions.html" title="Numerical Definitions"><link rel="refentry" href="glib-Miscellaneous-Macros.html" title="Miscellaneous Macros"><link rel="refentry" href="glib-Atomic-Operations.html" title="Atomic Operations"><link rel="chapter" href="glib-core.html" title="GLib Core Application Support"><link rel="refentry" href="glib-The-Main-Event-Loop.html" title="The Main Event Loop"><link rel="refentry" href="glib-Threads.html" title="
Threads"><link rel="refentry" href="glib-Thread-Pools.html" title="Thread Pools"><link rel="refentry" href="glib-Asynchronous-Queues.html" title="Asynchronous Queues"><link rel="refentry" href="glib-Dynamic-Loading-of-Modules.html" title="Dynamic Loading of Modules"><link rel="refentry" href="glib-Memory-Allocation.html" title="Memory Allocation"><link rel="refentry" href="glib-IO-Channels.html" title="IO Channels"><link rel="refentry" href="glib-Error-Reporting.html" title="Error Reporting"><link rel="refentry" href="glib-Warnings-and-Assertions.html" title="Message Output and Debugging Functions"><link rel="refentry" href="glib-Message-Logging.html" title="Message Logging"><link rel="chapter" href="glib-utilities.html" title="GLib Utilities"><link rel="refentry" href="glib-String-Utility-Functions.html" title="String Utility Functions"><link rel="refentry" href="glib-Character-Set-Conversion.html" title="Character Set Conversion"><link rel="refentry" href="glib-Unicode-Manipulation.html" title="Unicode Manipulation"><link rel="refentry" href="glib-I18N.html" title="Internationalization"><link rel="refentry" href="glib-Date-and-Time-Functions.html" title="Date and Time Functions"><link rel="refentry" href="glib-Random-Numbers.html" title="Random Numbers"><link rel="refentry" href="glib-Hook-Functions.html" title="Hook Functions"><link rel="refentry" href="glib-Miscellaneous-Utility-Functions.html" title="Miscellaneous Utility Functions"><link rel="refentry" href="glib-Lexical-Scanner.html" title="Lexical Scanner"><link rel="refentry" href="glib-Automatic-String-Completion.html" title="Automatic String Completion"><link rel="refentry" href="glib-Timers.html" title="Timers"><link rel="refentry" href="glib-Spawning-Processes.html" title="Spawning Processes"><link rel="refentry" href="glib-File-Utilities.html" title="File Utilities"><link rel="refentry" href="glib-Shell-related-Utilities.html" title="Shell-related Utilities"><link rel="refentry" href="glib-Glob-style-pattern-matching.html" title="Glob-style pattern matching"><link rel="refentry" href="glib-Simple-XML-Subset-Parser.html" title="Simple XML Subset Parser"><link rel="refentry" href="glib-Windows-Compatability-Functions.html" title="Windows Compatibility Functions"><link rel="chapter" href="glib-data-types.html" title="GLib Data Types"><link rel="refentry" href="glib-Memory-Chunks.html" title="Memory Chunks"><link rel="refentry" href="glib-Doubly-Linked-Lists.html" title="Doubly-Linked Lists"><link rel="refentry" href="glib-Singly-Linked-Lists.html" title="Singly-Linked Lists"><link rel="refentry" href="glib-Double-ended-Queues.html" title="Double-ended Queues"><link rel="refentry" href="glib-Trash-Stacks.html" title="Trash Stacks"><link rel="refentry" href="glib-Hash-Tables.html" title="Hash Tables"><link rel="refentry" href="glib-Strings.html" title="Strings"><link rel="refentry" href="glib-String-Chunks.html" title="String Chunks"><link rel="refentry" href="glib-Arrays.html" title="Arrays"><link rel="refentry" href="glib-Pointer-Arrays.html" title="Pointer Arrays"><link rel="refentry" href="glib-Byte-Arrays.html" title="Byte Arrays"><link rel="refentry" href="glib-Balanced-Binary-Trees.html" title="Balanced Binary Trees"><link rel="refentry" href="glib-N-ary-Trees.html" title="N-ary Trees"><link rel="refentry" href="glib-Quarks.html" title="Quarks"><link rel="refentry" href="glib-Keyed-Data-Lists.html" title="Keyed Data Lists"><link rel="refentry" href="glib-Datasets.html" title="Datasets"><link rel="refentry" href="glib-Relations-and-Tuples.html" title="Relations and Tuples"><link rel="refentry" href="glib-Caches.html" title="Caches"><link rel="refentry" href="glib-Memory-Allocators.html" title="Memory Allocators"><link rel="chapter" href="tools.html" title="GLib Tools"><link rel="refentry" href="glib-gettextize.html" title="glib-gettextize"><link rel="index" href="ix01.html" title="Index"><link rel="section" href="glib-Glob-style-pattern-matching.html#id3220620" title="Description"><link rel="section" href="glib-Glob-style-pattern-matching.html#id3220729" title="Details"><meta name="generator" content="GTK-Doc V1.2 (XML mode)"><link rel="stylesheet" href="style.css" type="text/css"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table class="navigation" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle"><td><a accesskey="p" href="glib-Shell-related-Utilities.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td><td><a accesskey="u" href="glib-utilities.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td><td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td><th width="100%" align="center">GLib Reference Manual</th><td><a accesskey="n" href="glib-Simple-XML-Subset-Parser.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td></tr></table><div class="refentry" lang="en"><a name="glib-Glob-style-pattern-matching"></a><div class="titlepage"><div></div><div></div></div><div class="refnamediv"><h2><span class="refentrytitle">Glob-style pattern matching</span></h2><p>Glob-style pattern matching &#8212; matches strings against patterns containing '*' (wildcard) and '?' (joker).</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">

#include &lt;glib.h&gt;


struct      <a href="glib-Glob-style-pattern-matching.html#GPatternSpec">GPatternSpec</a>;
<a href="glib-Glob-style-pattern-matching.html#GPatternSpec">GPatternSpec</a>* <a href="glib-Glob-style-pattern-matching.html#g-pattern-spec-new">g_pattern_spec_new</a>            (const <a href="glib-Basic-Types.html#gchar">gchar</a> *pattern);
void        <a href="glib-Glob-style-pattern-matching.html#g-pattern-spec-free">g_pattern_spec_free</a>             (<a href="glib-Glob-style-pattern-matching.html#GPatternSpec">GPatternSpec</a> *pspec);
<a href="glib-Basic-Types.html#gboolean">gboolean</a>    <a href="glib-Glob-style-pattern-matching.html#g-pattern-spec-equal">g_pattern_spec_equal</a>            (<a href="glib-Glob-style-pattern-matching.html#GPatternSpec">GPatternSpec</a> *pspec1,
                                             <a href="glib-Glob-style-pattern-matching.html#GPatternSpec">GPatternSpec</a> *pspec2);
<a href="glib-Basic-Types.html#gboolean">gboolean</a>    <a href="glib-Glob-style-pattern-matching.html#g-pattern-match">g_pattern_match</a>                 (<a href="glib-Glob-style-pattern-matching.html#GPatternSpec">GPatternSpec</a> *pspec,
                                             <a href="glib-Basic-Types.html#guint">guint</a> string_length,
                                             const <a href="glib-Basic-Types.html#gchar">gchar</a> *string,
                                             const <a href="glib-Basic-Types.html#gchar">gchar</a> *string_reversed);
<a href="glib-Basic-Types.html#gboolean">gboolean</a>    <a href="glib-Glob-style-pattern-matching.html#g-pattern-match-string">g_pattern_match_string</a>          (<a href="glib-Glob-style-pattern-matching.html#GPatternSpec">GPatternSpec</a> *pspec,
                                             const <a href="glib-Basic-Types.html#gchar">gchar</a> *string);
<a href="glib-Basic-Types.html#gboolean">gboolean</a>    <a href="glib-Glob-style-pattern-matching.html#g-pattern-match-simple">g_pattern_match_simple</a>          (const <a href="glib-Basic-Types.html#gchar">gchar</a> *pattern,
                                             const <a href="glib-Basic-Types.html#gchar">gchar</a> *string);
</pre></div><div class="refsect1" lang="en"><a name="id3220620"></a><h2>Description</h2><p>
The <tt class="function">g_pattern_match*</tt> functions match a string 
against a pattern containing '*' and '?' wildcards with similar semantics 
as the standard <tt class="function"><tt class="function">glob()</tt></tt> function: '*' matches an
arbitrary, possibly empty, string, '?' matches an arbitrary character.
</p><p>
Note that in contrast to <tt class="function"><tt class="function">glob()</tt></tt>, the '/' character
<span class="emphasis"><em>can</em></span> be matched by the wildcards, there are no
'[...]' character ranges and '*' and '?' can <span class="emphasis"><em>not</em></span>
be escaped to include them literally in a pattern. 
</p><p>
When multiple strings must be matched against the same pattern, it
is better to compile the pattern to a <a href="glib-Glob-style-pattern-matching.html#GPatternSpec"><span class="type">GPatternSpec</span></a> using 
<a href="glib-Glob-style-pattern-matching.html#g-pattern-spec-new"><tt class="function">g_pattern_spec_new()</tt></a> and use <a href="glib-Glob-style-pattern-matching.html#g-pattern-match-string"><tt class="function">g_pattern_match_string()</tt></a> instead of 
<a href="glib-Glob-style-pattern-matching.html#g-pattern-match-simple"><tt class="function">g_pattern_match_simple()</tt></a>.  This avoids the overhead of repeated 
pattern compilation.
</p></div><div class="refsect1" lang="en"><a name="id3220729"></a><h2>Details</h2><div class="refsect2" lang="en"><a name="id3220734"></a><h3><a name="GPatternSpec"></a>struct GPatternSpec</h3><a class="indexterm" name="id3220745"></a><pre class="programlisting">struct GPatternSpec;</pre><p>
A <span class="structname">GPatternSpec</span> is the 'compiled' form of a pattern.
This structure is opaque and its fields cannot be accessed directly.
</p></div><hr><div class="refsect2" lang="en"><a name="id3220766"></a><h3><a name="g-pattern-spec-new"></a>g_pattern_spec_new ()</h3><a class="indexterm" name="id3220778"></a><pre class="programlisting"><a href="glib-Glob-style-pattern-matching.html#GPatternSpec">GPatternSpec</a>* g_pattern_spec_new            (const <a href="glib-Basic-Types.html#gchar">gchar</a> *pattern);</pre><p>
Compiles a pattern to a <a href="glib-Glob-style-pattern-matching.html#GPatternSpec"><span class="type">GPatternSpec</span></a>.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>pattern</tt></i> :</span></td><td>a zero-terminated UTF-8 encoded string.
</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td>a newly-allocated <a href="glib-Glob-style-pattern-matching.html#GPatternSpec"><span class="type">GPatternSpec</span></a>.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3220852"></a><h3><a name="g-pattern-spec-free"></a>g_pattern_spec_free ()</h3><a class="indexterm" name="id3220863"></a><pre class="programlisting">void        g_pattern_spec_free             (<a href="glib-Glob-style-pattern-matching.html#GPatternSpec">GPatternSpec</a> *pspec);</pre><p>
Frees the memory allocated for the <a href="glib-Glob-style-pattern-matching.html#GPatternSpec"><span class="type">GPatternSpec</span></a>.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>pspec</tt></i> :</span></td><td>a <a href="glib-Glob-style-pattern-matching.html#GPatternSpec"><span class="type">GPatternSpec</span></a>.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3220926"></a><h3><a name="g-pattern-spec-equal"></a>g_pattern_spec_equal ()</h3><a class="indexterm" name="id3220938"></a><pre class="programlisting"><a href="glib-Basic-Types.html#gboolean">gboolean</a>    g_pattern_spec_equal            (<a href="glib-Glob-style-pattern-matching.html#GPatternSpec">GPatternSpec</a> *pspec1,
                                             <a href="glib-Glob-style-pattern-matching.html#GPatternSpec">GPatternSpec</a> *pspec2);</pre><p>
Compares two compiled pattern specs and returns whether they
will match the same set of strings.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>pspec1</tt></i> :</span></td><td>a <a href="glib-Glob-style-pattern-matching.html#GPatternSpec"><span class="type">GPatternSpec</span></a>.
</td></tr><tr><td><span class="term"><i class="parameter"><tt>pspec2</tt></i> :</span></td><td>another <a href="glib-Glob-style-pattern-matching.html#GPatternSpec"><span class="type">GPatternSpec</span></a>.
</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td>Whether the compiled patterns are equal.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3221035"></a><h3><a name="g-pattern-match"></a>g_pattern_match ()</h3><a class="indexterm" name="id3221046"></a><pre class="programlisting"><a href="glib-Basic-Types.html#gboolean">gboolean</a>    g_pattern_match                 (<a href="glib-Glob-style-pattern-matching.html#GPatternSpec">GPatternSpec</a> *pspec,
                                             <a href="glib-Basic-Types.html#guint">guint</a> string_length,
                                             const <a href="glib-Basic-Types.html#gchar">gchar</a> *string,
                                             const <a href="glib-Basic-Types.html#gchar">gchar</a> *string_reversed);</pre><p>
Matches a string against a compiled pattern. Passing the correct length of the
string given is mandatory. The reversed string can be omitted by passing <tt class="literal">NULL</tt>,
this is more efficient if the reversed version of the string to be matched is
not at hand, as <a href="glib-Glob-style-pattern-matching.html#g-pattern-match"><tt class="function">g_pattern_match()</tt></a> will only construct it if the compiled pattern
requires reverse matches.
</p><p>
Note that, if the user code will (possibly) match a string against a multitude 
of patterns containing wildcards, chances are high that some patterns will 
require a reversed string. In this case, it's more efficient to provide the 
reversed string to avoid multiple constructions thereof in the various calls to
<a href="glib-Glob-style-pattern-matching.html#g-pattern-match"><tt class="function">g_pattern_match()</tt></a>.
</p><p>
Note also that the reverse of a UTF-8 encoded string can in general 
<span class="emphasis"><em>not</em></span> be obtained by <tt class="function"><a href="glib-String-Utility-Functions.html#g-strreverse"><tt class="function">g_strreverse()</tt></a></tt>.
This works only if the string doesn't contain any multibyte characters.
Glib doesn't currently offer a function to reverse UTF-8 encoded strings.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>pspec</tt></i> :</span></td><td>a <a href="glib-Glob-style-pattern-matching.html#GPatternSpec"><span class="type">GPatternSpec</span></a>.
</td></tr><tr><td><span class="term"><i class="parameter"><tt>string_length</tt></i> :</span></td><td>the length of <i class="parameter"><tt>string</tt></i>.
</td></tr><tr><td><span class="term"><i class="parameter"><tt>string</tt></i> :</span></td><td>the UTF-8 encoded string to match.
</td></tr><tr><td><span class="term"><i class="parameter"><tt>string_reversed</tt></i> :</span></td><td>the reverse of <i class="parameter"><tt>string</tt></i> or <tt class="literal">NULL</tt>.
</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td><tt class="literal">TRUE</tt> if <i class="parameter"><tt>string</tt></i> matches <i class="parameter"><tt>pspec</tt></i>.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3221412"></a><h3><a name="g-pattern-match-string"></a>g_pattern_match_string ()</h3><a class="indexterm" name="id3221423"></a><pre class="programlisting"><a href="glib-Basic-Types.html#gboolean">gboolean</a>    g_pattern_match_string          (<a href="glib-Glob-style-pattern-matching.html#GPatternSpec">GPatternSpec</a> *pspec,
                                             const <a href="glib-Basic-Types.html#gchar">gchar</a> *string);</pre><p>
Matches a string against a compiled pattern. If the string is to
be matched against more than one pattern, consider using
<a href="glib-Glob-style-pattern-matching.html#g-pattern-match"><tt class="function">g_pattern_match()</tt></a> instead while supplying the reversed string.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>pspec</tt></i> :</span></td><td>a <a href="glib-Glob-style-pattern-matching.html#GPatternSpec"><span class="type">GPatternSpec</span></a>.
</td></tr><tr><td><span class="term"><i class="parameter"><tt>string</tt></i> :</span></td><td>the UTF-8 encoded string to match.
</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td><tt class="literal">TRUE</tt> if <i class="parameter"><tt>string</tt></i> matches <i class="parameter"><tt>pspec</tt></i>.


</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3221540"></a><h3><a name="g-pattern-match-simple"></a>g_pattern_match_simple ()</h3><a class="indexterm" name="id3221552"></a><pre class="programlisting"><a href="glib-Basic-Types.html#gboolean">gboolean</a>    g_pattern_match_simple          (const <a href="glib-Basic-Types.html#gchar">gchar</a> *pattern,
                                             const <a href="glib-Basic-Types.html#gchar">gchar</a> *string);</pre><p>
Matches a string against a pattern given as a string.
If this function is to be called in a loop, it's more efficient to compile
the pattern once with <a href="glib-Glob-style-pattern-matching.html#g-pattern-spec-new"><tt class="function">g_pattern_spec_new()</tt></a> and call <a href="glib-Glob-style-pattern-matching.html#g-pattern-match-string"><tt class="function">g_pattern_match_string()</tt></a>
repetively.
</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><i class="parameter"><tt>pattern</tt></i> :</span></td><td>the UTF-8 encoded pattern.
</td></tr><tr><td><span class="term"><i class="parameter"><tt>string</tt></i> :</span></td><td>the UTF-8 encoded string to match.
</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td><tt class="literal">TRUE</tt> if <i class="parameter"><tt>string</tt></i> matches <i class="parameter"><tt>pspec</tt></i>.


</td></tr></tbody></table></div></div></div></div><table class="navigation" width="100%" summary="Navigation footer" cellpadding="2" cellspacing="0"><tr valign="middle"><td align="left"><a accesskey="p" href="glib-Shell-related-Utilities.html"><b>&lt;&lt; Shell-related Utilities</b></a></td><td align="right"><a accesskey="n" href="glib-Simple-XML-Subset-Parser.html"><b>Simple XML Subset Parser &gt;&gt;</b></a></td></tr></table></body></html>