1 /* GRegex -- regular expression API wrapper around PCRE.
3 * Copyright (C) 1999, 2000 Scott Wimer
4 * Copyright (C) 2004, Matthias Clasen <mclasen@redhat.com>
5 * Copyright (C) 2005 - 2007, Marco Barisione <marco@barisione.org>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <glib/gi18n.h>
30 #ifdef USE_SYSTEM_PCRE
33 #include "pcre/pcre.h"
38 /* Mask of all the possible values for GRegexCompileFlags. */
39 #define G_REGEX_COMPILE_MASK (G_REGEX_CASELESS | \
44 G_REGEX_DOLLAR_ENDONLY | \
47 G_REGEX_NO_AUTO_CAPTURE | \
50 G_REGEX_NEWLINE_CR | \
51 G_REGEX_NEWLINE_LF | \
54 /* Mask of all the possible values for GRegexMatchFlags. */
55 #define G_REGEX_MATCH_MASK (G_REGEX_MATCH_ANCHORED | \
56 G_REGEX_MATCH_NOTBOL | \
57 G_REGEX_MATCH_NOTEOL | \
58 G_REGEX_MATCH_NOTEMPTY | \
59 G_REGEX_MATCH_PARTIAL | \
60 G_REGEX_MATCH_NEWLINE_CR | \
61 G_REGEX_MATCH_NEWLINE_LF | \
62 G_REGEX_MATCH_NEWLINE_CRLF | \
63 G_REGEX_MATCH_NEWLINE_ANY)
65 /* if the string is in UTF-8 use g_utf8_ functions, else use
67 #define NEXT_CHAR(re, s) (((re)->compile_opts & PCRE_UTF8) ? \
68 g_utf8_next_char (s) : \
70 #define PREV_CHAR(re, s) (((re)->compile_opts & PCRE_UTF8) ? \
71 g_utf8_prev_char (s) : \
76 GRegex *regex; /* the regex */
77 GRegexMatchFlags match_opts; /* options used at match time on the regex */
78 gint matches; /* number of matching sub patterns */
79 gint pos; /* position in the string where last match left off */
80 gint *offsets; /* array of offsets paired 0,1 ; 2,3 ; 3,4 etc */
81 gint n_offsets; /* number of offsets */
82 gint *workspace; /* workspace for pcre_dfa_exec() */
83 gint n_workspace; /* number of workspace elements */
84 const gchar *string; /* string passed to the match function */
85 gssize string_len; /* length of string */
90 volatile guint ref_count; /* the ref count for the immutable part */
91 gchar *pattern; /* the pattern */
92 pcre *pcre_re; /* compiled form of the pattern */
93 GRegexCompileFlags compile_opts; /* options used at compile time on the pattern */
94 GRegexMatchFlags match_opts; /* options used at match time on the regex */
95 pcre_extra *extra; /* data stored when G_REGEX_OPTIMIZE is used */
98 /* TRUE if ret is an error code, FALSE otherwise. */
99 #define IS_PCRE_ERROR(ret) ((ret) < PCRE_ERROR_NOMATCH && (ret) != PCRE_ERROR_PARTIAL)
101 static GRegex *regex_ref (GRegex *regex);
102 static void regex_unref (GRegex *regex);
104 typedef struct _InterpolationData InterpolationData;
105 static gboolean interpolate_replacement (const GRegex *regex,
106 const GMatchInfo *match_info,
110 static GList *split_replacement (const gchar *replacement,
112 static void free_interpolation_data (InterpolationData *data);
116 match_error (gint errcode)
120 case PCRE_ERROR_NOMATCH:
123 case PCRE_ERROR_NULL:
124 /* NULL argument, this should not happen in GRegex */
125 g_warning ("A NULL argument was passed to PCRE");
127 case PCRE_ERROR_BADOPTION:
128 return "bad options";
129 case PCRE_ERROR_BADMAGIC:
130 return _("corrupted object");
131 case PCRE_ERROR_UNKNOWN_OPCODE:
132 return N_("internal error or corrupted object");
133 case PCRE_ERROR_NOMEMORY:
134 return _("out of memory");
135 case PCRE_ERROR_NOSUBSTRING:
136 /* not used by pcre_exec() */
138 case PCRE_ERROR_MATCHLIMIT:
139 return _("backtracking limit reached");
140 case PCRE_ERROR_CALLOUT:
141 /* callouts are not implemented */
143 case PCRE_ERROR_BADUTF8:
144 case PCRE_ERROR_BADUTF8_OFFSET:
145 /* we do not check if strings are valid */
147 case PCRE_ERROR_PARTIAL:
150 case PCRE_ERROR_BADPARTIAL:
151 return _("the pattern contains items not supported for partial matching");
152 case PCRE_ERROR_INTERNAL:
153 return _("internal error");
154 case PCRE_ERROR_BADCOUNT:
155 /* negative ovecsize, this should not happen in GRegex */
156 g_warning ("A negative ovecsize was passed to PCRE");
158 case PCRE_ERROR_DFA_UITEM:
159 return _("the pattern contains items not supported for partial matching");
160 case PCRE_ERROR_DFA_UCOND:
161 return _("back references as conditions are not supported for partial matching");
162 case PCRE_ERROR_DFA_UMLIMIT:
163 /* the match_field field is not used in GRegex */
165 case PCRE_ERROR_DFA_WSSIZE:
166 /* handled expanding the workspace */
168 case PCRE_ERROR_DFA_RECURSE:
169 case PCRE_ERROR_RECURSIONLIMIT:
170 return _("recursion limit reached");
171 case PCRE_ERROR_NULLWSLIMIT:
172 return _("workspace limit for empty substrings reached");
173 case PCRE_ERROR_BADNEWLINE:
174 return _("invalid combination of newline flags");
178 return _("unknown error");
185 match_info_new (const GRegex *regex,
192 GMatchInfo *match_info;
195 string_len = strlen (string);
197 match_info = g_new0 (GMatchInfo, 1);
198 match_info->regex = regex_ref ((GRegex *)regex);
199 match_info->string = string;
200 match_info->string_len = string_len;
201 match_info->matches = PCRE_ERROR_NOMATCH;
202 match_info->pos = start_position;
203 match_info->match_opts = match_options;
207 /* These values should be enough for most cases, if they are not
208 * enough g_regex_match_all_full() will expand them. */
209 match_info->n_offsets = 24;
210 match_info->n_workspace = 100;
211 match_info->workspace = g_new (gint, match_info->n_workspace);
216 pcre_fullinfo (regex->pcre_re, regex->extra,
217 PCRE_INFO_CAPTURECOUNT, &capture_count);
218 match_info->n_offsets = (capture_count + 1) * 3;
220 match_info->offsets = g_new0 (gint, match_info->n_offsets);
227 * @match_info: a #GMatchInfo
229 * Frees all the memory associated with the #GMatchInfo structure.
234 g_match_info_free (GMatchInfo *match_info)
236 regex_unref (match_info->regex);
237 g_free (match_info->offsets);
238 g_free (match_info->workspace);
244 * @match_info: a #GMatchInfo structure
245 * @error: location to store the error occuring, or NULL to ignore errors
247 * Scans for the next match using the same parameters of the previous
248 * call to g_regex_match_full() or g_regex_match() that returned
251 * The match is done on the string passed to the match function, so you
252 * cannot free it before calling this function.
254 * Returns: %TRUE is the string matched, %FALSE otherwise
259 g_match_info_next (GMatchInfo *match_info,
262 g_return_val_if_fail (match_info != NULL, FALSE);
263 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
264 g_return_val_if_fail (match_info->pos >= 0, FALSE);
266 match_info->matches = pcre_exec (match_info->regex->pcre_re,
267 match_info->regex->extra,
269 match_info->string_len,
271 match_info->regex->match_opts |
272 match_info->match_opts,
274 match_info->n_offsets);
275 if (IS_PCRE_ERROR (match_info->matches))
277 g_set_error (error, G_REGEX_ERROR, G_REGEX_ERROR_MATCH,
278 _("Error while matching regular expression %s: %s"),
279 match_info->regex->pattern, match_error (match_info->matches));
283 /* avoid infinite loops if the pattern is an empty string or something
285 if (match_info->pos == match_info->offsets[1])
287 if (match_info->pos > match_info->string_len)
289 /* we have reached the end of the string */
290 match_info->pos = -1;
291 match_info->matches = PCRE_ERROR_NOMATCH;
294 match_info->pos = NEXT_CHAR (match_info->regex,
295 &match_info->string[match_info->pos]) -
300 match_info->pos = match_info->offsets[1];
303 return match_info->matches >= 0;
307 * g_match_info_matches:
308 * @match_info: a #GMatchInfo structure
310 * Returns: %TRUE if the previous match operation succeeded, %FALSE
316 g_match_info_matches (const GMatchInfo *match_info)
318 g_return_val_if_fail (match_info != NULL, FALSE);
320 return match_info->matches >= 0;
324 * g_match_info_get_match_count:
325 * @match_info: a #GMatchInfo structure
327 * Retrieves the number of matched substrings (including substring 0, that
328 * is the whole matched text), so 1 is returned if the pattern has no
329 * substrings in it and 0 is returned if the match failed.
331 * If the last match was obtained using the DFA algorithm, that is using
332 * g_regex_match_all() or g_regex_match_all_full(), the retrieved
333 * count is not that of the number of capturing parentheses but that of
334 * the number of matched substrings.
336 * Returns: Number of matched substrings, or -1 if an error occurred
341 g_match_info_get_match_count (const GMatchInfo *match_info)
343 g_return_val_if_fail (match_info, -1);
345 if (match_info->matches == PCRE_ERROR_NOMATCH)
348 else if (match_info->matches < PCRE_ERROR_NOMATCH)
353 return match_info->matches;
357 * g_match_info_is_partial_match:
358 * @match_info: a #GMatchInfo structure
360 * Usually if the string passed to g_regex_match*() matches as far as
361 * it goes, but is too short to match the entire pattern, %FALSE is
362 * returned. There are circumstances where it might be helpful to
363 * distinguish this case from other cases in which there is no match.
365 * Consider, for example, an application where a human is required to
366 * type in data for a field with specific formatting requirements. An
367 * example might be a date in the form ddmmmyy, defined by the pattern
368 * "^\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$".
369 * If the application sees the user’s keystrokes one by one, and can
370 * check that what has been typed so far is potentially valid, it is
371 * able to raise an error as soon as a mistake is made.
373 * GRegex supports the concept of partial matching by means of the
374 * #G_REGEX_MATCH_PARTIAL flag. When this is set the return code for
375 * g_regex_match() or g_regex_match_full() is, as usual, %TRUE
376 * for a complete match, %FALSE otherwise. But, when this functions
377 * returns %FALSE, you can check if the match was partial calling
378 * g_match_info_is_partial_match().
380 * When using partial matching you cannot use g_match_info_fetch*().
382 * Because of the way certain internal optimizations are implemented the
383 * partial matching algorithm cannot be used with all patterns. So repeated
384 * single characters such as "a{2,4}" and repeated single metasequences such
385 * as "\d+" are not permitted if the maximum number of occurrences is
386 * greater than one. Optional items such as "\d?" (where the maximum is one)
387 * are permitted. Quantifiers with any values are permitted after
388 * parentheses, so the invalid examples above can be coded thus "(a){2,4}"
389 * and "(\d)+". If #G_REGEX_MATCH_PARTIAL is set for a pattern that does
390 * not conform to the restrictions, matching functions return an error.
392 * Returns: %TRUE if the match was partial, %FALSE otherwise
397 g_match_info_is_partial_match (const GMatchInfo *match_info)
399 g_return_val_if_fail (match_info != NULL, FALSE);
401 return match_info->matches == PCRE_ERROR_PARTIAL;
405 * g_match_info_expand_references:
406 * @match_info: a #GMatchInfo
407 * @string_to_expand: the string to expand
408 * @error: location to store the error occuring, or %NULL to ignore errors
410 * Returns a new string containing the text in @string_to_expand with
411 * references expanded. References refer to the last match done with
412 * @string against @regex and have the same syntax used by g_regex_replace().
414 * The @string_to_expand must be UTF-8 encoded even if #G_REGEX_RAW was
415 * passed to g_regex_new().
417 * The backreferences are extracted from the string passed to the match
418 * function, so you cannot free it before calling this function.
420 * Returns: the expanded string, or %NULL if an error occurred
425 g_match_info_expand_references (const GMatchInfo *match_info,
426 const gchar *string_to_expand,
431 GError *tmp_error = NULL;
433 g_return_val_if_fail (match_info != NULL, NULL);
434 g_return_val_if_fail (string_to_expand != NULL, NULL);
435 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
437 list = split_replacement (string_to_expand, &tmp_error);
438 if (tmp_error != NULL)
440 g_propagate_error (error, tmp_error);
444 result = g_string_sized_new (strlen (string_to_expand));
445 interpolate_replacement (match_info->regex, match_info,
446 match_info->string, result, list);
448 g_list_foreach (list, (GFunc)free_interpolation_data, NULL);
451 return g_string_free (result, FALSE);
455 * g_match_info_fetch:
456 * @match_info: #GMatchInfo structure
457 * @match_num: number of the sub expression
459 * Retrieves the text matching the @match_num<!-- -->'th capturing parentheses.
460 * 0 is the full text of the match, 1 is the first paren set, 2 the second,
463 * If @match_num is a valid sub pattern but it didn't match anything (e.g.
464 * sub pattern 1, matching "b" against "(a)?b") then an empty string is
467 * If the match was obtained using the DFA algorithm, that is using
468 * g_regex_match_all() or g_regex_match_all_full(), the retrieved
469 * string is not that of a set of parentheses but that of a matched
470 * substring. Substrings are matched in reverse order of length, so 0 is
473 * The string is fetched from the string passed to the match function,
474 * so you cannot free it before calling this function.
476 * Returns: The matched substring, or %NULL if an error occurred.
477 * You have to free the string yourself
482 g_match_info_fetch (const GMatchInfo *match_info,
485 /* we cannot use pcre_get_substring() because it allocates the
486 * string using pcre_malloc(). */
490 g_return_val_if_fail (match_info != NULL, NULL);
491 g_return_val_if_fail (match_num >= 0, NULL);
493 /* match_num does not exist or it didn't matched, i.e. matching "b"
494 * against "(a)?b" then group 0 is empty. */
495 if (!g_match_info_fetch_pos (match_info, match_num, &start, &end))
497 else if (start == -1)
498 match = g_strdup ("");
500 match = g_strndup (&match_info->string[start], end - start);
506 * g_match_info_fetch_pos:
507 * @match_info: #GMatchInfo structure
508 * @match_num: number of the sub expression
509 * @start_pos: pointer to location where to store the start position
510 * @end_pos: pointer to location where to store the end position
512 * Retrieves the position of the @match_num<!-- -->'th capturing parentheses.
513 * 0 is the full text of the match, 1 is the first paren set, 2 the second,
516 * If @match_num is a valid sub pattern but it didn't match anything (e.g.
517 * sub pattern 1, matching "b" against "(a)?b") then @start_pos and @end_pos
518 * are set to -1 and %TRUE is returned.
520 * If the match was obtained using the DFA algorithm, that is using
521 * g_regex_match_all() or g_regex_match_all_full(), the retrieved
522 * position is not that of a set of parentheses but that of a matched
523 * substring. Substrings are matched in reverse order of length, so 0 is
526 * Returns: %TRUE if the position was fetched, %FALSE otherwise. If the
527 * position cannot be fetched, @start_pos and @end_pos are left
533 g_match_info_fetch_pos (const GMatchInfo *match_info,
538 g_return_val_if_fail (match_info != NULL, FALSE);
539 g_return_val_if_fail (match_num >= 0, FALSE);
541 /* make sure the sub expression number they're requesting is less than
542 * the total number of sub expressions that were matched. */
543 if (match_num >= match_info->matches)
546 if (start_pos != NULL)
547 *start_pos = match_info->offsets[2 * match_num];
550 *end_pos = match_info->offsets[2 * match_num + 1];
556 * g_match_info_fetch_named:
557 * @match_info: #GMatchInfo structure
558 * @name: name of the subexpression
560 * Retrieves the text matching the capturing parentheses named @name.
562 * If @name is a valid sub pattern name but it didn't match anything (e.g.
563 * sub pattern "X", matching "b" against "(?P<X>a)?b") then an empty
564 * string is returned.
566 * The string is fetched from the string passed to the match function,
567 * so you cannot free it before calling this function.
569 * Returns: The matched substring, or %NULL if an error occurred.
570 * You have to free the string yourself
575 g_match_info_fetch_named (const GMatchInfo *match_info,
578 /* we cannot use pcre_get_named_substring() because it allocates the
579 * string using pcre_malloc(). */
582 g_return_val_if_fail (match_info != NULL, NULL);
583 g_return_val_if_fail (name != NULL, NULL);
585 num = g_regex_get_string_number (match_info->regex, name);
589 return g_match_info_fetch (match_info, num);
593 * g_match_info_fetch_named_pos:
594 * @match_info: #GMatchInfo structure
595 * @name: name of the subexpression
596 * @start_pos: pointer to location where to store the start position
597 * @end_pos: pointer to location where to store the end position
599 * Retrieves the position of the capturing parentheses named @name.
601 * If @name is a valid sub pattern name but it didn't match anything (e.g.
602 * sub pattern "X", matching "b" against "(?P<X>a)?b") then @start_pos and
603 * @end_pos are set to -1 and %TRUE is returned.
605 * Returns: %TRUE if the position was fetched, %FALSE otherwise. If the
606 * position cannot be fetched, @start_pos and @end_pos are left
612 g_match_info_fetch_named_pos (const GMatchInfo *match_info,
619 g_return_val_if_fail (match_info != NULL, FALSE);
620 g_return_val_if_fail (name != NULL, FALSE);
622 num = g_regex_get_string_number (match_info->regex, name);
626 return g_match_info_fetch_pos (match_info, num, start_pos, end_pos);
630 * g_match_info_fetch_all:
631 * @match_info: a #GMatchInfo structure
633 * Bundles up pointers to each of the matching substrings from a match
634 * and stores them in an array of gchar pointers. The first element in
635 * the returned array is the match number 0, i.e. the entire matched
638 * If a sub pattern didn't match anything (e.g. sub pattern 1, matching
639 * "b" against "(a)?b") then an empty string is inserted.
641 * If the last match was obtained using the DFA algorithm, that is using
642 * g_regex_match_all() or g_regex_match_all_full(), the retrieved
643 * strings are not that matched by sets of parentheses but that of the
644 * matched substring. Substrings are matched in reverse order of length,
645 * so the first one is the longest match.
647 * The strings are fetched from the string passed to the match function,
648 * so you cannot free it before calling this function.
650 * Returns: a %NULL-terminated array of gchar * pointers. It must be freed
651 * using g_strfreev(). If the previous match failed %NULL is
657 g_match_info_fetch_all (const GMatchInfo *match_info)
659 /* we cannot use pcre_get_substring_list() because the returned value
660 * isn't suitable for g_strfreev(). */
664 g_return_val_if_fail (match_info != NULL, FALSE);
666 if (match_info->matches < 0)
669 result = g_new (gchar *, match_info->matches + 1);
670 for (i = 0; i < match_info->matches; i++)
671 result[i] = g_match_info_fetch (match_info, i);
681 g_regex_error_quark (void)
683 static GQuark error_quark = 0;
685 if (error_quark == 0)
686 error_quark = g_quark_from_static_string ("g-regex-error-quark");
692 regex_ref (GRegex *regex)
694 g_atomic_int_inc ((gint*) ®ex->ref_count);
699 regex_unref (GRegex *regex)
701 if (g_atomic_int_exchange_and_add ((gint *) ®ex->ref_count, -1) - 1 == 0)
703 g_free (regex->pattern);
704 if (regex->pcre_re != NULL)
705 pcre_free (regex->pcre_re);
706 if (regex->extra != NULL)
707 pcre_free (regex->extra);
714 * @pattern: the regular expression
715 * @compile_options: compile options for the regular expression
716 * @match_options: match options for the regular expression
717 * @error: return location for a #GError
719 * Compiles the regular expression to an internal form, and does the initial
720 * setup of the #GRegex structure.
722 * Returns: a #GRegex structure
727 g_regex_new (const gchar *pattern,
728 GRegexCompileFlags compile_options,
729 GRegexMatchFlags match_options,
736 gboolean optimize = FALSE;
737 static gboolean initialized = FALSE;
739 g_return_val_if_fail (pattern != NULL, NULL);
740 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
741 g_return_val_if_fail ((compile_options & ~G_REGEX_COMPILE_MASK) == 0, NULL);
742 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
749 pcre_config (PCRE_CONFIG_UTF8, &support);
752 msg = N_("PCRE library is compiled without UTF8 support");
754 g_set_error (error, G_REGEX_ERROR, G_REGEX_ERROR_COMPILE, gettext (msg));
758 pcre_config (PCRE_CONFIG_UNICODE_PROPERTIES, &support);
761 msg = N_("PCRE library is compiled without UTF8 properties support");
763 g_set_error (error, G_REGEX_ERROR, G_REGEX_ERROR_COMPILE, gettext (msg));
770 /* G_REGEX_OPTIMIZE has the same numeric value of PCRE_NO_UTF8_CHECK,
771 * as we do not need to wrap PCRE_NO_UTF8_CHECK. */
772 if (compile_options & G_REGEX_OPTIMIZE)
775 /* In GRegex the string are, by default, UTF-8 encoded. PCRE
776 * instead uses UTF-8 only if required with PCRE_UTF8. */
777 if (compile_options & G_REGEX_RAW)
780 compile_options &= ~G_REGEX_RAW;
785 compile_options |= PCRE_UTF8 | PCRE_NO_UTF8_CHECK;
786 match_options |= PCRE_NO_UTF8_CHECK;
789 /* PCRE_NEWLINE_ANY is the default for the internal PCRE but
790 * not for the system one. */
791 if (!(compile_options & G_REGEX_NEWLINE_CR) &&
792 !(compile_options & G_REGEX_NEWLINE_LF))
794 compile_options |= PCRE_NEWLINE_ANY;
797 /* compile the pattern */
798 re = pcre_compile (pattern, compile_options, &errmsg, &erroffset, NULL);
800 /* if the compilation failed, set the error member and return
804 GError *tmp_error = g_error_new (G_REGEX_ERROR,
805 G_REGEX_ERROR_COMPILE,
806 _("Error while compiling regular "
807 "expression %s at char %d: %s"),
808 pattern, erroffset, errmsg);
809 g_propagate_error (error, tmp_error);
814 regex = g_new0 (GRegex, 1);
815 regex->ref_count = 1;
816 regex->pattern = g_strdup (pattern);
818 regex->compile_opts = compile_options;
819 regex->match_opts = match_options;
823 regex->extra = pcre_study (regex->pcre_re, 0, &errmsg);
826 GError *tmp_error = g_error_new (G_REGEX_ERROR,
827 G_REGEX_ERROR_OPTIMIZE,
828 _("Error while optimizing "
829 "regular expression %s: %s"),
832 g_propagate_error (error, tmp_error);
844 * Frees all the memory associated with the regex structure.
849 g_regex_free (GRegex *regex)
858 * g_regex_get_pattern:
859 * @regex: a #GRegex structure
861 * Gets the pattern string associated with @regex, i.e. a copy of
862 * the string passed to g_regex_new().
864 * Returns: the pattern of @regex
869 g_regex_get_pattern (const GRegex *regex)
871 g_return_val_if_fail (regex != NULL, NULL);
873 return regex->pattern;
877 * g_regex_match_simple:
878 * @pattern: the regular expression
879 * @string: the string to scan for matches
880 * @compile_options: compile options for the regular expression
881 * @match_options: match options
883 * Scans for a match in @string for @pattern.
885 * This function is equivalent to g_regex_match() but it does not
886 * require to compile the pattern with g_regex_new(), avoiding some
887 * lines of code when you need just to do a match without extracting
888 * substrings, capture counts, and so on.
890 * If this function is to be called on the same @pattern more than
891 * once, it's more efficient to compile the pattern once with
892 * g_regex_new() and then use g_regex_match().
894 * Returns: %TRUE is the string matched, %FALSE otherwise
899 g_regex_match_simple (const gchar *pattern,
901 GRegexCompileFlags compile_options,
902 GRegexMatchFlags match_options)
907 regex = g_regex_new (pattern, compile_options, 0, NULL);
910 result = g_regex_match_full (regex, string, -1, 0, match_options, NULL, NULL);
911 g_regex_free (regex);
917 * @regex: a #GRegex structure from g_regex_new()
918 * @string: the string to scan for matches
919 * @match_options: match options
920 * @match_info: pointer to location where to store the #GMatchInfo, or
921 * %NULL if you do not nedd it
923 * Scans for a match in string for the pattern in @regex. The @match_options
924 * are combined with the match options specified when the @regex structure
925 * was created, letting you have more flexibility in reusing #GRegex
928 * A #GMatchInfo structure, used to get information on the match, is stored
929 * in @match_info if not %NULL.
931 * To retrieve all the non-overlapping matches of the pattern in string you
932 * can use g_match_info_next().
934 * <informalexample><programlisting>
936 * print_uppercase_words (const gchar *string)
938 * /* Print all uppercase-only words. */
940 * GMatchInfo *match_info;
942 * regex = g_regex_new ("[A-Z]+", 0, 0, NULL);
943 * g_regex_match (regex, string, 0, &match_info);
944 * while (g_match_info_matches (match_info))
946 * gchar *word = g_match_info_fetch (match_info, 0);
947 * g_print ("Found: %s\n", word);
949 * g_match_info_next (match_info, NULL);
951 * g_match_info_free (match_info);
952 * g_regex_free (regex);
954 * </programlisting></informalexample>
956 * Returns: %TRUE is the string matched, %FALSE otherwise
961 g_regex_match (const GRegex *regex,
963 GRegexMatchFlags match_options,
964 GMatchInfo **match_info)
966 return g_regex_match_full (regex, string, -1, 0, match_options,
971 * g_regex_match_full:
972 * @regex: a #GRegex structure from g_regex_new()
973 * @string: the string to scan for matches
974 * @string_len: the length of @string, or -1 if @string is nul-terminated
975 * @start_position: starting index of the string to match
976 * @match_options: match options
977 * @match_info: pointer to location where to store the #GMatchInfo, or
978 * %NULL if you do not nedd it
979 * @error: location to store the error occuring, or %NULL to ignore errors
981 * Scans for a match in string for the pattern in @regex. The @match_options
982 * are combined with the match options specified when the @regex structure
983 * was created, letting you have more flexibility in reusing #GRegex
986 * Setting @start_position differs from just passing over a shortened string
987 * and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern that begins
988 * with any kind of lookbehind assertion, such as "\b".
990 * A #GMatchInfo structure, used to get information on the match, is stored
991 * in @match_info if not %NULL.
993 * To retrieve all the non-overlapping matches of the pattern in string you
994 * can use g_match_info_next().
996 * <informalexample><programlisting>
998 * print_uppercase_words (const gchar *string)
1000 * /* Print all uppercase-only words. */
1002 * GMatchInfo *match_info;
1003 * GError *error = NULL;
1005 * regex = g_regex_new ("[A-Z]+", 0, 0, NULL);
1006 * g_regex_match_full (regex, string, -1, 0, 0, &match_info, &error);
1007 * while (g_match_info_matches (match_info))
1009 * gchar *word = g_match_info_fetch (match_info, 0);
1010 * g_print ("Found: %s\n", word);
1012 * g_match_info_next (match_info, &error);
1014 * g_match_info_free (match_info);
1015 * g_regex_free (regex);
1016 * if (error != NULL)
1018 * g_printerr ("Error while matching: %s\n", error->message);
1019 * g_error_free (error);
1022 * </programlisting></informalexample>
1024 * Returns: %TRUE is the string matched, %FALSE otherwise
1029 g_regex_match_full (const GRegex *regex,
1030 const gchar *string,
1032 gint start_position,
1033 GRegexMatchFlags match_options,
1034 GMatchInfo **match_info,
1040 g_return_val_if_fail (regex != NULL, FALSE);
1041 g_return_val_if_fail (string != NULL, FALSE);
1042 g_return_val_if_fail (start_position >= 0, FALSE);
1043 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1044 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, FALSE);
1046 info = match_info_new (regex, string, string_len, start_position,
1047 match_options, FALSE);
1048 match_ok = g_match_info_next (info, error);
1049 if (match_info != NULL)
1052 g_match_info_free (info);
1058 * g_regex_match_all:
1059 * @regex: a #GRegex structure from g_regex_new()
1060 * @string: the string to scan for matches
1061 * @match_options: match options
1062 * @match_info: pointer to location where to store the #GMatchInfo, or
1063 * %NULL if you do not nedd it
1065 * Using the standard algorithm for regular expression matching only the
1066 * longest match in the string is retrieved. This function uses a
1067 * different algorithm so it can retrieve all the possible matches.
1068 * For more documentation see g_regex_match_all_full().
1070 * A #GMatchInfo structure, used to get information on the match, is stored
1071 * in @match_info if not %NULL.
1073 * Returns: %TRUE is the string matched, %FALSE otherwise
1078 g_regex_match_all (const GRegex *regex,
1079 const gchar *string,
1080 GRegexMatchFlags match_options,
1081 GMatchInfo **match_info)
1083 return g_regex_match_all_full (regex, string, -1, 0, match_options,
1088 * g_regex_match_all_full:
1089 * @regex: a #GRegex structure from g_regex_new()
1090 * @string: the string to scan for matches
1091 * @string_len: the length of @string, or -1 if @string is nul-terminated
1092 * @start_position: starting index of the string to match
1093 * @match_options: match options
1094 * @match_info: pointer to location where to store the #GMatchInfo, or
1095 * %NULL if you do not nedd it
1096 * @error: location to store the error occuring, or %NULL to ignore errors
1098 * Using the standard algorithm for regular expression matching only the
1099 * longest match in the string is retrieved, it is not possibile to obtain
1100 * all the available matches. For instance matching
1101 * "<a> <b> <c>" against the pattern "<.*>" you get
1102 * "<a> <b> <c>".
1104 * This function uses a different algorithm (called DFA, i.e. deterministic
1105 * finite automaton), so it can retrieve all the possible matches, all
1106 * starting at the same point in the string. For instance matching
1107 * "<a> <b> <c>" against the pattern "<.*>" you
1108 * would obtain three matches: "<a> <b> <c>",
1109 * "<a> <b>" and "<a>".
1111 * The number of matched strings is retrieved using
1112 * g_match_info_get_match_count().
1113 * To obtain the matched strings and their position you can use,
1114 * respectively, g_match_info_fetch() and g_match_info_fetch_pos(). Note that
1115 * the strings are returned in reverse order of length; that is, the longest
1116 * matching string is given first.
1118 * Note that the DFA algorithm is slower than the standard one and it is not
1119 * able to capture substrings, so backreferences do not work.
1121 * Setting @start_position differs from just passing over a shortened string
1122 * and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern that begins
1123 * with any kind of lookbehind assertion, such as "\b".
1125 * A #GMatchInfo structure, used to get information on the match, is stored
1126 * in @match_info if not %NULL.
1128 * Returns: %TRUE is the string matched, %FALSE otherwise
1133 g_regex_match_all_full (const GRegex *regex,
1134 const gchar *string,
1136 gint start_position,
1137 GRegexMatchFlags match_options,
1138 GMatchInfo **match_info,
1144 g_return_val_if_fail (regex != NULL, FALSE);
1145 g_return_val_if_fail (string != NULL, FALSE);
1146 g_return_val_if_fail (start_position >= 0, FALSE);
1147 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1148 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, FALSE);
1150 info = match_info_new (regex, string, string_len, start_position,
1151 match_options, TRUE);
1157 info->matches = pcre_dfa_exec (regex->pcre_re, regex->extra,
1158 info->string, info->string_len,
1160 regex->match_opts | match_options,
1161 info->offsets, info->n_offsets,
1162 info->workspace, info->n_workspace);
1163 if (info->matches == PCRE_ERROR_DFA_WSSIZE)
1165 /* info->workspace is too small. */
1166 info->n_workspace *= 2;
1167 info->workspace = g_realloc (info->workspace,
1168 info->n_workspace * sizeof (gint));
1171 else if (info->matches == 0)
1173 /* info->offsets is too small. */
1174 info->n_offsets *= 2;
1175 info->offsets = g_realloc (info->offsets,
1176 info->n_offsets * sizeof (gint));
1179 else if (IS_PCRE_ERROR (info->matches))
1181 g_set_error (error, G_REGEX_ERROR, G_REGEX_ERROR_MATCH,
1182 _("Error while matching regular expression %s: %s"),
1183 regex->pattern, match_error (info->matches));
1187 /* set info->pos to -1 so that a call to g_match_info_next() fails. */
1190 if (match_info != NULL)
1193 g_match_info_free (info);
1195 return info->matches >= 0;
1199 * g_regex_get_string_number:
1200 * @regex: #GRegex structure
1201 * @name: name of the subexpression
1203 * Retrieves the number of the subexpression named @name.
1205 * Returns: The number of the subexpression or -1 if @name does not exists
1210 g_regex_get_string_number (const GRegex *regex,
1215 g_return_val_if_fail (regex != NULL, -1);
1216 g_return_val_if_fail (name != NULL, -1);
1218 num = pcre_get_stringnumber (regex->pcre_re, name);
1219 if (num == PCRE_ERROR_NOSUBSTRING)
1226 * g_regex_split_simple:
1227 * @pattern: the regular expression
1228 * @string: the string to scan for matches
1229 * @compile_options: compile options for the regular expression
1230 * @match_options: match options
1232 * Breaks the string on the pattern, and returns an array of the tokens.
1233 * If the pattern contains capturing parentheses, then the text for each
1234 * of the substrings will also be returned. If the pattern does not match
1235 * anywhere in the string, then the whole string is returned as the first
1238 * This function is equivalent to g_regex_split() but it does not
1239 * require to compile the pattern with g_regex_new(), avoiding some
1240 * lines of code when you need just to do a split without extracting
1241 * substrings, capture counts, and so on.
1243 * If this function is to be called on the same @pattern more than
1244 * once, it's more efficient to compile the pattern once with
1245 * g_regex_new() and then use g_regex_split().
1247 * As a special case, the result of splitting the empty string "" is an
1248 * empty vector, not a vector containing a single string. The reason for
1249 * this special case is that being able to represent a empty vector is
1250 * typically more useful than consistent handling of empty elements. If
1251 * you do need to represent empty elements, you'll need to check for the
1252 * empty string before calling this function.
1254 * A pattern that can match empty strings splits @string into separate
1255 * characters wherever it matches the empty string between characters.
1256 * For example splitting "ab c" using as a separator "\s*", you will get
1259 * Returns: a %NULL-terminated gchar ** array. Free it using g_strfreev()
1264 g_regex_split_simple (const gchar *pattern,
1265 const gchar *string,
1266 GRegexCompileFlags compile_options,
1267 GRegexMatchFlags match_options)
1272 regex = g_regex_new (pattern, compile_options, 0, NULL);
1275 result = g_regex_split_full (regex, string, -1, 0, match_options, 0, NULL);
1276 g_regex_free (regex);
1282 * @regex: a #GRegex structure
1283 * @string: the string to split with the pattern
1284 * @match_options: match time option flags
1286 * Breaks the string on the pattern, and returns an array of the tokens.
1287 * If the pattern contains capturing parentheses, then the text for each
1288 * of the substrings will also be returned. If the pattern does not match
1289 * anywhere in the string, then the whole string is returned as the first
1292 * As a special case, the result of splitting the empty string "" is an
1293 * empty vector, not a vector containing a single string. The reason for
1294 * this special case is that being able to represent a empty vector is
1295 * typically more useful than consistent handling of empty elements. If
1296 * you do need to represent empty elements, you'll need to check for the
1297 * empty string before calling this function.
1299 * A pattern that can match empty strings splits @string into separate
1300 * characters wherever it matches the empty string between characters.
1301 * For example splitting "ab c" using as a separator "\s*", you will get
1304 * Returns: a %NULL-terminated gchar ** array. Free it using g_strfreev()
1309 g_regex_split (const GRegex *regex,
1310 const gchar *string,
1311 GRegexMatchFlags match_options)
1313 return g_regex_split_full (regex, string, -1, 0,
1314 match_options, 0, NULL);
1318 * g_regex_split_full:
1319 * @regex: a #GRegex structure
1320 * @string: the string to split with the pattern
1321 * @string_len: the length of @string, or -1 if @string is nul-terminated
1322 * @start_position: starting index of the string to match
1323 * @match_options: match time option flags
1324 * @max_tokens: the maximum number of tokens to split @string into. If this
1325 * is less than 1, the string is split completely
1326 * @error: return location for a #GError
1328 * Breaks the string on the pattern, and returns an array of the tokens.
1329 * If the pattern contains capturing parentheses, then the text for each
1330 * of the substrings will also be returned. If the pattern does not match
1331 * anywhere in the string, then the whole string is returned as the first
1334 * As a special case, the result of splitting the empty string "" is an
1335 * empty vector, not a vector containing a single string. The reason for
1336 * this special case is that being able to represent a empty vector is
1337 * typically more useful than consistent handling of empty elements. If
1338 * you do need to represent empty elements, you'll need to check for the
1339 * empty string before calling this function.
1341 * A pattern that can match empty strings splits @string into separate
1342 * characters wherever it matches the empty string between characters.
1343 * For example splitting "ab c" using as a separator "\s*", you will get
1346 * Setting @start_position differs from just passing over a shortened string
1347 * and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern that begins
1348 * with any kind of lookbehind assertion, such as "\b".
1350 * Returns: a %NULL-terminated gchar ** array. Free it using g_strfreev()
1355 g_regex_split_full (const GRegex *regex,
1356 const gchar *string,
1358 gint start_position,
1359 GRegexMatchFlags match_options,
1363 GError *tmp_error = NULL;
1364 GMatchInfo *match_info;
1369 /* position of the last separator. */
1370 gint last_separator_end;
1371 /* was the last match 0 bytes long? */
1372 gboolean last_match_is_empty;
1373 /* the returned array of char **s */
1374 gchar **string_list;
1376 g_return_val_if_fail (regex != NULL, NULL);
1377 g_return_val_if_fail (string != NULL, NULL);
1378 g_return_val_if_fail (start_position >= 0, NULL);
1379 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1380 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
1382 if (max_tokens <= 0)
1383 max_tokens = G_MAXINT;
1386 string_len = strlen (string);
1388 /* zero-length string */
1389 if (string_len - start_position == 0)
1390 return g_new0 (gchar *, 1);
1392 if (max_tokens == 1)
1394 string_list = g_new0 (gchar *, 2);
1395 string_list[0] = g_strndup (&string[start_position],
1396 string_len - start_position);
1402 last_separator_end = start_position;
1403 last_match_is_empty = FALSE;
1405 match_ok = g_regex_match_full (regex, string, string_len, start_position,
1406 match_options, &match_info, &tmp_error);
1407 while (tmp_error == NULL)
1411 last_match_is_empty =
1412 (match_info->offsets[0] == match_info->offsets[1]);
1414 /* we need to skip empty separators at the same position of the end
1415 * of another separator. e.g. the string is "a b" and the separator
1416 * is " *", so from 1 to 2 we have a match and at position 2 we have
1417 * an empty match. */
1418 if (last_separator_end != match_info->offsets[1])
1423 token = g_strndup (string + last_separator_end,
1424 match_info->offsets[0] - last_separator_end);
1425 list = g_list_prepend (list, token);
1428 /* if there were substrings, these need to be added to
1430 match_count = g_match_info_get_match_count (match_info);
1431 if (match_count > 1)
1433 for (i = 1; i < match_count; i++)
1434 list = g_list_prepend (list, g_match_info_fetch (match_info, i));
1440 /* if there was no match, copy to end of string. */
1441 if (!last_match_is_empty)
1443 gchar *token = g_strndup (string + last_separator_end,
1444 match_info->string_len - last_separator_end);
1445 list = g_list_prepend (list, token);
1447 /* no more tokens, end the loop. */
1451 /* -1 to leave room for the last part. */
1452 if (token_count >= max_tokens - 1)
1454 /* we have reached the maximum number of tokens, so we copy
1455 * the remaining part of the string. */
1456 if (last_match_is_empty)
1458 /* the last match was empty, so we have moved one char
1459 * after the real position to avoid empty matches at the
1461 match_info->pos = PREV_CHAR (regex, &string[match_info->pos]) - string;
1463 /* the if is needed in the case we have terminated the available
1464 * tokens, but we are at the end of the string, so there are no
1465 * characters left to copy. */
1466 if (string_len > match_info->pos)
1468 gchar *token = g_strndup (string + match_info->pos,
1469 string_len - match_info->pos);
1470 list = g_list_prepend (list, token);
1476 last_separator_end = match_info->pos;
1477 if (last_match_is_empty)
1478 /* if the last match was empty, g_match_info_next() has moved
1479 * forward to avoid infinite loops, but we still need to copy that
1481 last_separator_end = PREV_CHAR (regex, &string[last_separator_end]) - string;
1483 match_ok = g_match_info_next (match_info, &tmp_error);
1485 g_match_info_free (match_info);
1486 if (tmp_error != NULL)
1488 g_propagate_error (error, tmp_error);
1489 g_list_foreach (list, (GFunc)g_free, NULL);
1491 match_info->pos = -1;
1495 string_list = g_new (gchar *, g_list_length (list) + 1);
1497 for (last = g_list_last (list); last; last = g_list_previous (last))
1498 string_list[i++] = last->data;
1508 REPL_TYPE_CHARACTER,
1509 REPL_TYPE_SYMBOLIC_REFERENCE,
1510 REPL_TYPE_NUMERIC_REFERENCE,
1511 REPL_TYPE_CHANGE_CASE
1516 CHANGE_CASE_NONE = 1 << 0,
1517 CHANGE_CASE_UPPER = 1 << 1,
1518 CHANGE_CASE_LOWER = 1 << 2,
1519 CHANGE_CASE_UPPER_SINGLE = 1 << 3,
1520 CHANGE_CASE_LOWER_SINGLE = 1 << 4,
1521 CHANGE_CASE_SINGLE_MASK = CHANGE_CASE_UPPER_SINGLE | CHANGE_CASE_LOWER_SINGLE,
1522 CHANGE_CASE_LOWER_MASK = CHANGE_CASE_LOWER | CHANGE_CASE_LOWER_SINGLE,
1523 CHANGE_CASE_UPPER_MASK = CHANGE_CASE_UPPER | CHANGE_CASE_UPPER_SINGLE
1526 struct _InterpolationData
1532 ChangeCase change_case;
1536 free_interpolation_data (InterpolationData *data)
1538 g_free (data->text);
1542 static const gchar *
1543 expand_escape (const gchar *replacement,
1545 InterpolationData *data,
1550 const gchar *error_detail;
1552 GError *tmp_error = NULL;
1560 data->type = REPL_TYPE_CHARACTER;
1565 data->type = REPL_TYPE_CHARACTER;
1570 data->type = REPL_TYPE_CHARACTER;
1575 data->type = REPL_TYPE_CHARACTER;
1580 data->type = REPL_TYPE_CHARACTER;
1585 data->type = REPL_TYPE_CHARACTER;
1590 data->type = REPL_TYPE_CHARACTER;
1595 data->type = REPL_TYPE_CHARACTER;
1605 h = g_ascii_xdigit_value (*p);
1608 error_detail = _("hexadecimal digit or '}' expected");
1619 for (i = 0; i < 2; i++)
1621 h = g_ascii_xdigit_value (*p);
1624 error_detail = _("hexadecimal digit expected");
1631 data->type = REPL_TYPE_STRING;
1632 data->text = g_new0 (gchar, 8);
1633 g_unichar_to_utf8 (x, data->text);
1637 data->type = REPL_TYPE_CHANGE_CASE;
1638 data->change_case = CHANGE_CASE_LOWER_SINGLE;
1642 data->type = REPL_TYPE_CHANGE_CASE;
1643 data->change_case = CHANGE_CASE_UPPER_SINGLE;
1647 data->type = REPL_TYPE_CHANGE_CASE;
1648 data->change_case = CHANGE_CASE_LOWER;
1652 data->type = REPL_TYPE_CHANGE_CASE;
1653 data->change_case = CHANGE_CASE_UPPER;
1657 data->type = REPL_TYPE_CHANGE_CASE;
1658 data->change_case = CHANGE_CASE_NONE;
1664 error_detail = _("missing '<' in symbolic reference");
1673 error_detail = _("unfinished symbolic reference");
1680 error_detail = _("zero-length symbolic reference");
1683 if (g_ascii_isdigit (*q))
1688 h = g_ascii_digit_value (*q);
1691 error_detail = _("digit expected");
1700 data->type = REPL_TYPE_NUMERIC_REFERENCE;
1707 if (!g_ascii_isalnum (*r))
1709 error_detail = _("illegal symbolic reference");
1716 data->text = g_strndup (q, p - q);
1717 data->type = REPL_TYPE_SYMBOLIC_REFERENCE;
1722 /* if \0 is followed by a number is an octal number representing a
1723 * character, else it is a numeric reference. */
1724 if (g_ascii_digit_value (*g_utf8_next_char (p)) >= 0)
1727 p = g_utf8_next_char (p);
1740 for (i = 0; i < 3; i++)
1742 h = g_ascii_digit_value (*p);
1752 if (i == 2 && base == 10)
1758 if (base == 8 || i == 3)
1760 data->type = REPL_TYPE_STRING;
1761 data->text = g_new0 (gchar, 8);
1762 g_unichar_to_utf8 (x, data->text);
1766 data->type = REPL_TYPE_NUMERIC_REFERENCE;
1771 error_detail = _("stray final '\\'");
1775 error_detail = _("unknown escape sequence");
1782 /* G_GSSIZE_FORMAT doesn't work with gettext, so we use %lu */
1783 tmp_error = g_error_new (G_REGEX_ERROR,
1784 G_REGEX_ERROR_REPLACE,
1785 _("Error while parsing replacement "
1786 "text \"%s\" at char %lu: %s"),
1788 (gulong)(p - replacement),
1790 g_propagate_error (error, tmp_error);
1796 split_replacement (const gchar *replacement,
1800 InterpolationData *data;
1801 const gchar *p, *start;
1803 start = p = replacement;
1808 data = g_new0 (InterpolationData, 1);
1809 start = p = expand_escape (replacement, p, data, error);
1812 g_list_foreach (list, (GFunc)free_interpolation_data, NULL);
1814 free_interpolation_data (data);
1818 list = g_list_prepend (list, data);
1823 if (*p == '\\' || *p == '\0')
1827 data = g_new0 (InterpolationData, 1);
1828 data->text = g_strndup (start, p - start);
1829 data->type = REPL_TYPE_STRING;
1830 list = g_list_prepend (list, data);
1836 return g_list_reverse (list);
1839 /* Change the case of c based on change_case. */
1840 #define CHANGE_CASE(c, change_case) \
1841 (((change_case) & CHANGE_CASE_LOWER_MASK) ? \
1842 g_unichar_tolower (c) : \
1843 g_unichar_toupper (c))
1846 string_append (GString *string,
1848 ChangeCase *change_case)
1852 if (text[0] == '\0')
1855 if (*change_case == CHANGE_CASE_NONE)
1857 g_string_append (string, text);
1859 else if (*change_case & CHANGE_CASE_SINGLE_MASK)
1861 c = g_utf8_get_char (text);
1862 g_string_append_unichar (string, CHANGE_CASE (c, *change_case));
1863 g_string_append (string, g_utf8_next_char (text));
1864 *change_case = CHANGE_CASE_NONE;
1868 while (*text != '\0')
1870 c = g_utf8_get_char (text);
1871 g_string_append_unichar (string, CHANGE_CASE (c, *change_case));
1872 text = g_utf8_next_char (text);
1878 interpolate_replacement (const GRegex *regex,
1879 const GMatchInfo *match_info,
1880 const gchar *string,
1885 InterpolationData *idata;
1887 ChangeCase change_case = CHANGE_CASE_NONE;
1889 for (list = data; list; list = list->next)
1892 switch (idata->type)
1894 case REPL_TYPE_STRING:
1895 string_append (result, idata->text, &change_case);
1897 case REPL_TYPE_CHARACTER:
1898 g_string_append_c (result, CHANGE_CASE (idata->c, change_case));
1899 if (change_case & CHANGE_CASE_SINGLE_MASK)
1900 change_case = CHANGE_CASE_NONE;
1902 case REPL_TYPE_NUMERIC_REFERENCE:
1903 match = g_match_info_fetch (match_info, idata->num);
1906 string_append (result, match, &change_case);
1910 case REPL_TYPE_SYMBOLIC_REFERENCE:
1911 match = g_match_info_fetch_named (match_info, idata->text);
1914 string_append (result, match, &change_case);
1918 case REPL_TYPE_CHANGE_CASE:
1919 change_case = idata->change_case;
1929 * @regex: a #GRegex structure
1930 * @string: the string to perform matches against
1931 * @string_len: the length of @string, or -1 if @string is nul-terminated
1932 * @start_position: starting index of the string to match
1933 * @replacement: text to replace each match with
1934 * @match_options: options for the match
1935 * @error: location to store the error occuring, or %NULL to ignore errors
1937 * Replaces all occurances of the pattern in @regex with the
1938 * replacement text. Backreferences of the form '\number' or '\g<number>'
1939 * in the replacement text are interpolated by the number-th captured
1940 * subexpression of the match, '\g<name>' refers to the captured subexpression
1941 * with the given name. '\0' refers to the complete match, but '\0' followed
1942 * by a number is the octal representation of a character. To include a
1943 * literal '\' in the replacement, write '\\'.
1944 * There are also escapes that changes the case of the following text:
1947 * <varlistentry><term>\l</term>
1949 * <para>Convert to lower case the next character</para>
1952 * <varlistentry><term>\u</term>
1954 * <para>Convert to upper case the next character</para>
1957 * <varlistentry><term>\L</term>
1959 * <para>Convert to lower case till \E</para>
1962 * <varlistentry><term>\U</term>
1964 * <para>Convert to upper case till \E</para>
1967 * <varlistentry><term>\E</term>
1969 * <para>End case modification</para>
1974 * If you do not need to use backreferences use g_regex_replace_literal().
1976 * The @replacement string must be UTF-8 encoded even if #G_REGEX_RAW was
1977 * passed to g_regex_new(). If you want to use not UTF-8 encoded stings
1978 * you can use g_regex_replace_literal().
1980 * Setting @start_position differs from just passing over a shortened string
1981 * and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern that begins
1982 * with any kind of lookbehind assertion, such as "\b".
1984 * Returns: a newly allocated string containing the replacements
1989 g_regex_replace (const GRegex *regex,
1990 const gchar *string,
1992 gint start_position,
1993 const gchar *replacement,
1994 GRegexMatchFlags match_options,
1999 GError *tmp_error = NULL;
2001 g_return_val_if_fail (regex != NULL, NULL);
2002 g_return_val_if_fail (string != NULL, NULL);
2003 g_return_val_if_fail (start_position >= 0, NULL);
2004 g_return_val_if_fail (replacement != NULL, NULL);
2005 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2006 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
2008 list = split_replacement (replacement, &tmp_error);
2009 if (tmp_error != NULL)
2011 g_propagate_error (error, tmp_error);
2015 result = g_regex_replace_eval (regex,
2016 string, string_len, start_position,
2018 interpolate_replacement,
2021 if (tmp_error != NULL)
2022 g_propagate_error (error, tmp_error);
2024 g_list_foreach (list, (GFunc)free_interpolation_data, NULL);
2031 literal_replacement (const GRegex *regex,
2032 const GMatchInfo *match_info,
2033 const gchar *string,
2037 g_string_append (result, data);
2042 * g_regex_replace_literal:
2043 * @regex: a #GRegex structure
2044 * @string: the string to perform matches against
2045 * @string_len: the length of @string, or -1 if @string is nul-terminated
2046 * @start_position: starting index of the string to match
2047 * @replacement: text to replace each match with
2048 * @match_options: options for the match
2049 * @error: location to store the error occuring, or %NULL to ignore errors
2051 * Replaces all occurances of the pattern in @regex with the
2052 * replacement text. @replacement is replaced literally, to
2053 * include backreferences use g_regex_replace().
2055 * Setting @start_position differs from just passing over a shortened string
2056 * and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern that begins
2057 * with any kind of lookbehind assertion, such as "\b".
2059 * Returns: a newly allocated string containing the replacements
2064 g_regex_replace_literal (const GRegex *regex,
2065 const gchar *string,
2067 gint start_position,
2068 const gchar *replacement,
2069 GRegexMatchFlags match_options,
2072 g_return_val_if_fail (replacement != NULL, NULL);
2073 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
2075 return g_regex_replace_eval (regex,
2076 string, string_len, start_position,
2078 literal_replacement,
2079 (gpointer)replacement,
2084 * g_regex_replace_eval:
2085 * @regex: a #GRegex structure from g_regex_new()
2086 * @string: string to perform matches against
2087 * @string_len: the length of @string, or -1 if @string is nul-terminated
2088 * @start_position: starting index of the string to match
2089 * @match_options: options for the match
2090 * @eval: a function to call for each match
2091 * @user_data: user data to pass to the function
2092 * @error: location to store the error occuring, or %NULL to ignore errors
2094 * Replaces occurances of the pattern in regex with the output of @eval
2095 * for that occurance.
2097 * Setting @start_position differs from just passing over a shortened string
2098 * and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern that begins
2099 * with any kind of lookbehind assertion, such as "\b".
2101 * Returns: a newly allocated string containing the replacements
2106 g_regex_replace_eval (const GRegex *regex,
2107 const gchar *string,
2109 gint start_position,
2110 GRegexMatchFlags match_options,
2111 GRegexEvalCallback eval,
2115 GMatchInfo *match_info;
2118 gboolean done = FALSE;
2119 GError *tmp_error = NULL;
2121 g_return_val_if_fail (regex != NULL, NULL);
2122 g_return_val_if_fail (string != NULL, NULL);
2123 g_return_val_if_fail (start_position >= 0, NULL);
2124 g_return_val_if_fail (eval != NULL, NULL);
2125 g_return_val_if_fail ((match_options & ~G_REGEX_MATCH_MASK) == 0, NULL);
2128 string_len = strlen (string);
2130 result = g_string_sized_new (string_len);
2132 /* run down the string making matches. */
2133 g_regex_match_full (regex, string, string_len, start_position,
2134 match_options, &match_info, &tmp_error);
2135 while (!done && g_match_info_matches (match_info))
2137 g_string_append_len (result,
2139 match_info->offsets[0] - str_pos);
2140 done = (*eval) (regex, match_info, string, result, user_data);
2141 str_pos = match_info->offsets[1];
2142 g_match_info_next (match_info, &tmp_error);
2144 g_match_info_free (match_info);
2145 if (tmp_error != NULL)
2147 g_propagate_error (error, tmp_error);
2148 g_string_free (result, TRUE);
2152 g_string_append_len (result, string + str_pos, string_len - str_pos);
2153 return g_string_free (result, FALSE);
2157 * g_regex_escape_string:
2158 * @string: the string to escape
2159 * @length: the length of @string, or -1 if @string is nul-terminated
2161 * Escapes the special characters used for regular expressions in @string,
2162 * for instance "a.b*c" becomes "a\.b\*c". This function is useful to
2163 * dynamically generate regular expressions.
2165 * @string can contain nul characters that are replaced with "\0", in this
2166 * case remember to specify the correct length of @string in @length.
2168 * Returns: a newly-allocated escaped string
2173 g_regex_escape_string (const gchar *string,
2177 const char *p, *piece_start, *end;
2179 g_return_val_if_fail (string != NULL, NULL);
2182 length = strlen (string);
2184 end = string + length;
2185 p = piece_start = string;
2186 escaped = g_string_sized_new (length + 1);
2207 if (p != piece_start)
2208 /* copy the previous piece. */
2209 g_string_append_len (escaped, piece_start, p - piece_start);
2210 g_string_append_c (escaped, '\\');
2212 g_string_append_c (escaped, '0');
2214 g_string_append_c (escaped, *p);
2218 p = g_utf8_next_char (p);
2223 if (piece_start < end)
2224 g_string_append_len (escaped, piece_start, end - piece_start);
2226 return g_string_free (escaped, FALSE);
2229 #define __G_REGEX_C__
2230 #include "galiasdef.c"