1 /* 2 * This file is part of gtkD. 3 * 4 * gtkD is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License 6 * as published by the Free Software Foundation; either version 3 7 * of the License, or (at your option) any later version, with 8 * some exceptions, please read the COPYING file. 9 * 10 * gtkD is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with gtkD; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 18 */ 19 20 // generated automatically - do not change 21 // find conversion definition on APILookup.txt 22 // implement new conversion functionalities on the wrap.utils pakage 23 24 25 module glib.Regex; 26 27 private import glib.ConstructionException; 28 private import glib.ErrorG; 29 private import glib.GException; 30 private import glib.MatchInfo; 31 private import glib.Str; 32 private import glib.c.functions; 33 public import glib.c.types; 34 private import gtkd.Loader; 35 36 37 /** 38 * The g_regex_*() functions implement regular 39 * expression pattern matching using syntax and semantics similar to 40 * Perl regular expression. 41 * 42 * Some functions accept a @start_position argument, setting it differs 43 * from just passing over a shortened string and setting #G_REGEX_MATCH_NOTBOL 44 * in the case of a pattern that begins with any kind of lookbehind assertion. 45 * For example, consider the pattern "\Biss\B" which finds occurrences of "iss" 46 * in the middle of words. ("\B" matches only if the current position in the 47 * subject is not a word boundary.) When applied to the string "Mississipi" 48 * from the fourth byte, namely "issipi", it does not match, because "\B" is 49 * always false at the start of the subject, which is deemed to be a word 50 * boundary. However, if the entire string is passed , but with 51 * @start_position set to 4, it finds the second occurrence of "iss" because 52 * it is able to look behind the starting point to discover that it is 53 * preceded by a letter. 54 * 55 * Note that, unless you set the #G_REGEX_RAW flag, all the strings passed 56 * to these functions must be encoded in UTF-8. The lengths and the positions 57 * inside the strings are in bytes and not in characters, so, for instance, 58 * "\xc3\xa0" (i.e. "à") is two bytes long but it is treated as a 59 * single character. If you set #G_REGEX_RAW the strings can be non-valid 60 * UTF-8 strings and a byte is treated as a character, so "\xc3\xa0" is two 61 * bytes and two characters long. 62 * 63 * When matching a pattern, "\n" matches only against a "\n" character in 64 * the string, and "\r" matches only a "\r" character. To match any newline 65 * sequence use "\R". This particular group matches either the two-character 66 * sequence CR + LF ("\r\n"), or one of the single characters LF (linefeed, 67 * U+000A, "\n"), VT vertical tab, U+000B, "\v"), FF (formfeed, U+000C, "\f"), 68 * CR (carriage return, U+000D, "\r"), NEL (next line, U+0085), LS (line 69 * separator, U+2028), or PS (paragraph separator, U+2029). 70 * 71 * The behaviour of the dot, circumflex, and dollar metacharacters are 72 * affected by newline characters, the default is to recognize any newline 73 * character (the same characters recognized by "\R"). This can be changed 74 * with #G_REGEX_NEWLINE_CR, #G_REGEX_NEWLINE_LF and #G_REGEX_NEWLINE_CRLF 75 * compile options, and with #G_REGEX_MATCH_NEWLINE_ANY, 76 * #G_REGEX_MATCH_NEWLINE_CR, #G_REGEX_MATCH_NEWLINE_LF and 77 * #G_REGEX_MATCH_NEWLINE_CRLF match options. These settings are also 78 * relevant when compiling a pattern if #G_REGEX_EXTENDED is set, and an 79 * unescaped "#" outside a character class is encountered. This indicates 80 * a comment that lasts until after the next newline. 81 * 82 * When setting the %G_REGEX_JAVASCRIPT_COMPAT flag, pattern syntax and pattern 83 * matching is changed to be compatible with the way that regular expressions 84 * work in JavaScript. More precisely, a lonely ']' character in the pattern 85 * is a syntax error; the '\x' escape only allows 0 to 2 hexadecimal digits, and 86 * you must use the '\u' escape sequence with 4 hex digits to specify a unicode 87 * codepoint instead of '\x' or 'x{....}'. If '\x' or '\u' are not followed by 88 * the specified number of hex digits, they match 'x' and 'u' literally; also 89 * '\U' always matches 'U' instead of being an error in the pattern. Finally, 90 * pattern matching is modified so that back references to an unset subpattern 91 * group produces a match with the empty string instead of an error. See 92 * pcreapi(3) for more information. 93 * 94 * Creating and manipulating the same #GRegex structure from different 95 * threads is not a problem as #GRegex does not modify its internal 96 * state between creation and destruction, on the other hand #GMatchInfo 97 * is not threadsafe. 98 * 99 * The regular expressions low-level functionalities are obtained through 100 * the excellent 101 * [PCRE](http://www.pcre.org/) 102 * library written by Philip Hazel. 103 * 104 * Since: 2.14 105 */ 106 public class Regex 107 { 108 /** the main Gtk struct */ 109 protected GRegex* gRegex; 110 protected bool ownedRef; 111 112 /** Get the main Gtk struct */ 113 public GRegex* getRegexStruct(bool transferOwnership = false) 114 { 115 if (transferOwnership) 116 ownedRef = false; 117 return gRegex; 118 } 119 120 /** the main Gtk struct as a void* */ 121 protected void* getStruct() 122 { 123 return cast(void*)gRegex; 124 } 125 126 /** 127 * Sets our main struct and passes it to the parent class. 128 */ 129 public this (GRegex* gRegex, bool ownedRef = false) 130 { 131 this.gRegex = gRegex; 132 this.ownedRef = ownedRef; 133 } 134 135 ~this () 136 { 137 if ( Linker.isLoaded(LIBRARY_GLIB) && ownedRef ) 138 g_regex_unref(gRegex); 139 } 140 141 142 /** 143 * Compiles the regular expression to an internal form, and does 144 * the initial setup of the #GRegex structure. 145 * 146 * Params: 147 * pattern = the regular expression 148 * compileOptions = compile options for the regular expression, or 0 149 * matchOptions = match options for the regular expression, or 0 150 * 151 * Returns: a #GRegex structure or %NULL if an error occurred. Call 152 * g_regex_unref() when you are done with it 153 * 154 * Since: 2.14 155 * 156 * Throws: GException on failure. 157 * Throws: ConstructionException GTK+ fails to create the object. 158 */ 159 public this(string pattern, GRegexCompileFlags compileOptions, GRegexMatchFlags matchOptions) 160 { 161 GError* err = null; 162 163 auto __p = g_regex_new(Str.toStringz(pattern), compileOptions, matchOptions, &err); 164 165 if (err !is null) 166 { 167 throw new GException( new ErrorG(err) ); 168 } 169 170 if(__p is null) 171 { 172 throw new ConstructionException("null returned by new"); 173 } 174 175 this(cast(GRegex*) __p); 176 } 177 178 /** 179 * Returns the number of capturing subpatterns in the pattern. 180 * 181 * Returns: the number of capturing subpatterns 182 * 183 * Since: 2.14 184 */ 185 public int getCaptureCount() 186 { 187 return g_regex_get_capture_count(gRegex); 188 } 189 190 /** 191 * Returns the compile options that @regex was created with. 192 * 193 * Depending on the version of PCRE that is used, this may or may not 194 * include flags set by option expressions such as `(?i)` found at the 195 * top-level within the compiled pattern. 196 * 197 * Returns: flags from #GRegexCompileFlags 198 * 199 * Since: 2.26 200 */ 201 public GRegexCompileFlags getCompileFlags() 202 { 203 return g_regex_get_compile_flags(gRegex); 204 } 205 206 /** 207 * Checks whether the pattern contains explicit CR or LF references. 208 * 209 * Returns: %TRUE if the pattern contains explicit CR or LF references 210 * 211 * Since: 2.34 212 */ 213 public bool getHasCrOrLf() 214 { 215 return g_regex_get_has_cr_or_lf(gRegex) != 0; 216 } 217 218 /** 219 * Returns the match options that @regex was created with. 220 * 221 * Returns: flags from #GRegexMatchFlags 222 * 223 * Since: 2.26 224 */ 225 public GRegexMatchFlags getMatchFlags() 226 { 227 return g_regex_get_match_flags(gRegex); 228 } 229 230 /** 231 * Returns the number of the highest back reference 232 * in the pattern, or 0 if the pattern does not contain 233 * back references. 234 * 235 * Returns: the number of the highest back reference 236 * 237 * Since: 2.14 238 */ 239 public int getMaxBackref() 240 { 241 return g_regex_get_max_backref(gRegex); 242 } 243 244 /** 245 * Gets the number of characters in the longest lookbehind assertion in the 246 * pattern. This information is useful when doing multi-segment matching using 247 * the partial matching facilities. 248 * 249 * Returns: the number of characters in the longest lookbehind assertion. 250 * 251 * Since: 2.38 252 */ 253 public int getMaxLookbehind() 254 { 255 return g_regex_get_max_lookbehind(gRegex); 256 } 257 258 /** 259 * Gets the pattern string associated with @regex, i.e. a copy of 260 * the string passed to g_regex_new(). 261 * 262 * Returns: the pattern of @regex 263 * 264 * Since: 2.14 265 */ 266 public string getPattern() 267 { 268 return Str.toString(g_regex_get_pattern(gRegex)); 269 } 270 271 /** 272 * Retrieves the number of the subexpression named @name. 273 * 274 * Params: 275 * name = name of the subexpression 276 * 277 * Returns: The number of the subexpression or -1 if @name 278 * does not exists 279 * 280 * Since: 2.14 281 */ 282 public int getStringNumber(string name) 283 { 284 return g_regex_get_string_number(gRegex, Str.toStringz(name)); 285 } 286 287 /** 288 * Scans for a match in @string for the pattern in @regex. 289 * The @match_options are combined with the match options specified 290 * when the @regex structure was created, letting you have more 291 * flexibility in reusing #GRegex structures. 292 * 293 * Unless %G_REGEX_RAW is specified in the options, @string must be valid UTF-8. 294 * 295 * A #GMatchInfo structure, used to get information on the match, 296 * is stored in @match_info if not %NULL. Note that if @match_info 297 * is not %NULL then it is created even if the function returns %FALSE, 298 * i.e. you must free it regardless if regular expression actually matched. 299 * 300 * To retrieve all the non-overlapping matches of the pattern in 301 * string you can use g_match_info_next(). 302 * 303 * |[<!-- language="C" --> 304 * static void 305 * print_uppercase_words (const gchar *string) 306 * { 307 * // Print all uppercase-only words. 308 * GRegex *regex; 309 * GMatchInfo *match_info; 310 * 311 * regex = g_regex_new ("[A-Z]+", 0, 0, NULL); 312 * g_regex_match (regex, string, 0, &match_info); 313 * while (g_match_info_matches (match_info)) 314 * { 315 * gchar *word = g_match_info_fetch (match_info, 0); 316 * g_print ("Found: %s\n", word); 317 * g_free (word); 318 * g_match_info_next (match_info, NULL); 319 * } 320 * g_match_info_free (match_info); 321 * g_regex_unref (regex); 322 * } 323 * ]| 324 * 325 * @string is not copied and is used in #GMatchInfo internally. If 326 * you use any #GMatchInfo method (except g_match_info_free()) after 327 * freeing or modifying @string then the behaviour is undefined. 328 * 329 * Params: 330 * string_ = the string to scan for matches 331 * matchOptions = match options 332 * matchInfo = pointer to location where to store 333 * the #GMatchInfo, or %NULL if you do not need it 334 * 335 * Returns: %TRUE is the string matched, %FALSE otherwise 336 * 337 * Since: 2.14 338 */ 339 public bool match(string string_, GRegexMatchFlags matchOptions, out MatchInfo matchInfo) 340 { 341 GMatchInfo* outmatchInfo = null; 342 343 auto __p = g_regex_match(gRegex, Str.toStringz(string_), matchOptions, &outmatchInfo) != 0; 344 345 matchInfo = new MatchInfo(outmatchInfo); 346 347 return __p; 348 } 349 350 /** 351 * Using the standard algorithm for regular expression matching only 352 * the longest match in the string is retrieved. This function uses 353 * a different algorithm so it can retrieve all the possible matches. 354 * For more documentation see g_regex_match_all_full(). 355 * 356 * A #GMatchInfo structure, used to get information on the match, is 357 * stored in @match_info if not %NULL. Note that if @match_info is 358 * not %NULL then it is created even if the function returns %FALSE, 359 * i.e. you must free it regardless if regular expression actually 360 * matched. 361 * 362 * @string is not copied and is used in #GMatchInfo internally. If 363 * you use any #GMatchInfo method (except g_match_info_free()) after 364 * freeing or modifying @string then the behaviour is undefined. 365 * 366 * Params: 367 * string_ = the string to scan for matches 368 * matchOptions = match options 369 * matchInfo = pointer to location where to store 370 * the #GMatchInfo, or %NULL if you do not need it 371 * 372 * Returns: %TRUE is the string matched, %FALSE otherwise 373 * 374 * Since: 2.14 375 */ 376 public bool matchAll(string string_, GRegexMatchFlags matchOptions, out MatchInfo matchInfo) 377 { 378 GMatchInfo* outmatchInfo = null; 379 380 auto __p = g_regex_match_all(gRegex, Str.toStringz(string_), matchOptions, &outmatchInfo) != 0; 381 382 matchInfo = new MatchInfo(outmatchInfo); 383 384 return __p; 385 } 386 387 /** 388 * Using the standard algorithm for regular expression matching only 389 * the longest match in the @string is retrieved, it is not possible 390 * to obtain all the available matches. For instance matching 391 * "<a> <b> <c>" against the pattern "<.*>" 392 * you get "<a> <b> <c>". 393 * 394 * This function uses a different algorithm (called DFA, i.e. deterministic 395 * finite automaton), so it can retrieve all the possible matches, all 396 * starting at the same point in the string. For instance matching 397 * "<a> <b> <c>" against the pattern "<.*>;" 398 * you would obtain three matches: "<a> <b> <c>", 399 * "<a> <b>" and "<a>". 400 * 401 * The number of matched strings is retrieved using 402 * g_match_info_get_match_count(). To obtain the matched strings and 403 * their position you can use, respectively, g_match_info_fetch() and 404 * g_match_info_fetch_pos(). Note that the strings are returned in 405 * reverse order of length; that is, the longest matching string is 406 * given first. 407 * 408 * Note that the DFA algorithm is slower than the standard one and it 409 * is not able to capture substrings, so backreferences do not work. 410 * 411 * Setting @start_position differs from just passing over a shortened 412 * string and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern 413 * that begins with any kind of lookbehind assertion, such as "\b". 414 * 415 * Unless %G_REGEX_RAW is specified in the options, @string must be valid UTF-8. 416 * 417 * A #GMatchInfo structure, used to get information on the match, is 418 * stored in @match_info if not %NULL. Note that if @match_info is 419 * not %NULL then it is created even if the function returns %FALSE, 420 * i.e. you must free it regardless if regular expression actually 421 * matched. 422 * 423 * @string is not copied and is used in #GMatchInfo internally. If 424 * you use any #GMatchInfo method (except g_match_info_free()) after 425 * freeing or modifying @string then the behaviour is undefined. 426 * 427 * Params: 428 * string_ = the string to scan for matches 429 * startPosition = starting index of the string to match, in bytes 430 * matchOptions = match options 431 * matchInfo = pointer to location where to store 432 * the #GMatchInfo, or %NULL if you do not need it 433 * 434 * Returns: %TRUE is the string matched, %FALSE otherwise 435 * 436 * Since: 2.14 437 * 438 * Throws: GException on failure. 439 */ 440 public bool matchAllFull(string string_, int startPosition, GRegexMatchFlags matchOptions, out MatchInfo matchInfo) 441 { 442 GMatchInfo* outmatchInfo = null; 443 GError* err = null; 444 445 auto __p = g_regex_match_all_full(gRegex, Str.toStringz(string_), cast(ptrdiff_t)string_.length, startPosition, matchOptions, &outmatchInfo, &err) != 0; 446 447 if (err !is null) 448 { 449 throw new GException( new ErrorG(err) ); 450 } 451 452 matchInfo = new MatchInfo(outmatchInfo); 453 454 return __p; 455 } 456 457 /** 458 * Scans for a match in @string for the pattern in @regex. 459 * The @match_options are combined with the match options specified 460 * when the @regex structure was created, letting you have more 461 * flexibility in reusing #GRegex structures. 462 * 463 * Setting @start_position differs from just passing over a shortened 464 * string and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern 465 * that begins with any kind of lookbehind assertion, such as "\b". 466 * 467 * Unless %G_REGEX_RAW is specified in the options, @string must be valid UTF-8. 468 * 469 * A #GMatchInfo structure, used to get information on the match, is 470 * stored in @match_info if not %NULL. Note that if @match_info is 471 * not %NULL then it is created even if the function returns %FALSE, 472 * i.e. you must free it regardless if regular expression actually 473 * matched. 474 * 475 * @string is not copied and is used in #GMatchInfo internally. If 476 * you use any #GMatchInfo method (except g_match_info_free()) after 477 * freeing or modifying @string then the behaviour is undefined. 478 * 479 * To retrieve all the non-overlapping matches of the pattern in 480 * string you can use g_match_info_next(). 481 * 482 * |[<!-- language="C" --> 483 * static void 484 * print_uppercase_words (const gchar *string) 485 * { 486 * // Print all uppercase-only words. 487 * GRegex *regex; 488 * GMatchInfo *match_info; 489 * GError *error = NULL; 490 * 491 * regex = g_regex_new ("[A-Z]+", 0, 0, NULL); 492 * g_regex_match_full (regex, string, -1, 0, 0, &match_info, &error); 493 * while (g_match_info_matches (match_info)) 494 * { 495 * gchar *word = g_match_info_fetch (match_info, 0); 496 * g_print ("Found: %s\n", word); 497 * g_free (word); 498 * g_match_info_next (match_info, &error); 499 * } 500 * g_match_info_free (match_info); 501 * g_regex_unref (regex); 502 * if (error != NULL) 503 * { 504 * g_printerr ("Error while matching: %s\n", error->message); 505 * g_error_free (error); 506 * } 507 * } 508 * ]| 509 * 510 * Params: 511 * string_ = the string to scan for matches 512 * startPosition = starting index of the string to match, in bytes 513 * matchOptions = match options 514 * matchInfo = pointer to location where to store 515 * the #GMatchInfo, or %NULL if you do not need it 516 * 517 * Returns: %TRUE is the string matched, %FALSE otherwise 518 * 519 * Since: 2.14 520 * 521 * Throws: GException on failure. 522 */ 523 public bool matchFull(string string_, int startPosition, GRegexMatchFlags matchOptions, out MatchInfo matchInfo) 524 { 525 GMatchInfo* outmatchInfo = null; 526 GError* err = null; 527 528 auto __p = g_regex_match_full(gRegex, Str.toStringz(string_), cast(ptrdiff_t)string_.length, startPosition, matchOptions, &outmatchInfo, &err) != 0; 529 530 if (err !is null) 531 { 532 throw new GException( new ErrorG(err) ); 533 } 534 535 matchInfo = new MatchInfo(outmatchInfo); 536 537 return __p; 538 } 539 540 alias doref = ref_; 541 /** 542 * Increases reference count of @regex by 1. 543 * 544 * Returns: @regex 545 * 546 * Since: 2.14 547 */ 548 public Regex ref_() 549 { 550 auto __p = g_regex_ref(gRegex); 551 552 if(__p is null) 553 { 554 return null; 555 } 556 557 return new Regex(cast(GRegex*) __p, true); 558 } 559 560 /** 561 * Replaces all occurrences of the pattern in @regex with the 562 * replacement text. Backreferences of the form '\number' or 563 * '\g<number>' in the replacement text are interpolated by the 564 * number-th captured subexpression of the match, '\g<name>' refers 565 * to the captured subexpression with the given name. '\0' refers 566 * to the complete match, but '\0' followed by a number is the octal 567 * representation of a character. To include a literal '\' in the 568 * replacement, write '\\\\'. 569 * 570 * There are also escapes that changes the case of the following text: 571 * 572 * - \l: Convert to lower case the next character 573 * - \u: Convert to upper case the next character 574 * - \L: Convert to lower case till \E 575 * - \U: Convert to upper case till \E 576 * - \E: End case modification 577 * 578 * If you do not need to use backreferences use g_regex_replace_literal(). 579 * 580 * The @replacement string must be UTF-8 encoded even if #G_REGEX_RAW was 581 * passed to g_regex_new(). If you want to use not UTF-8 encoded strings 582 * you can use g_regex_replace_literal(). 583 * 584 * Setting @start_position differs from just passing over a shortened 585 * string and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern that 586 * begins with any kind of lookbehind assertion, such as "\b". 587 * 588 * Params: 589 * string_ = the string to perform matches against 590 * startPosition = starting index of the string to match, in bytes 591 * replacement = text to replace each match with 592 * matchOptions = options for the match 593 * 594 * Returns: a newly allocated string containing the replacements 595 * 596 * Since: 2.14 597 * 598 * Throws: GException on failure. 599 */ 600 public string replace(string string_, int startPosition, string replacement, GRegexMatchFlags matchOptions) 601 { 602 GError* err = null; 603 604 auto retStr = g_regex_replace(gRegex, Str.toStringz(string_), cast(ptrdiff_t)string_.length, startPosition, Str.toStringz(replacement), matchOptions, &err); 605 606 if (err !is null) 607 { 608 throw new GException( new ErrorG(err) ); 609 } 610 611 scope(exit) Str.freeString(retStr); 612 return Str.toString(retStr); 613 } 614 615 /** 616 * Replaces occurrences of the pattern in regex with the output of 617 * @eval for that occurrence. 618 * 619 * Setting @start_position differs from just passing over a shortened 620 * string and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern 621 * that begins with any kind of lookbehind assertion, such as "\b". 622 * 623 * The following example uses g_regex_replace_eval() to replace multiple 624 * strings at once: 625 * |[<!-- language="C" --> 626 * static gboolean 627 * eval_cb (const GMatchInfo *info, 628 * GString *res, 629 * gpointer data) 630 * { 631 * gchar *match; 632 * gchar *r; 633 * 634 * match = g_match_info_fetch (info, 0); 635 * r = g_hash_table_lookup ((GHashTable *)data, match); 636 * g_string_append (res, r); 637 * g_free (match); 638 * 639 * return FALSE; 640 * } 641 * 642 * ... 643 * 644 * GRegex *reg; 645 * GHashTable *h; 646 * gchar *res; 647 * 648 * h = g_hash_table_new (g_str_hash, g_str_equal); 649 * 650 * g_hash_table_insert (h, "1", "ONE"); 651 * g_hash_table_insert (h, "2", "TWO"); 652 * g_hash_table_insert (h, "3", "THREE"); 653 * g_hash_table_insert (h, "4", "FOUR"); 654 * 655 * reg = g_regex_new ("1|2|3|4", 0, 0, NULL); 656 * res = g_regex_replace_eval (reg, text, -1, 0, 0, eval_cb, h, NULL); 657 * g_hash_table_destroy (h); 658 * 659 * ... 660 * ]| 661 * 662 * Params: 663 * string_ = string to perform matches against 664 * startPosition = starting index of the string to match, in bytes 665 * matchOptions = options for the match 666 * eval = a function to call for each match 667 * userData = user data to pass to the function 668 * 669 * Returns: a newly allocated string containing the replacements 670 * 671 * Since: 2.14 672 * 673 * Throws: GException on failure. 674 */ 675 public string replaceEval(string string_, int startPosition, GRegexMatchFlags matchOptions, GRegexEvalCallback eval, void* userData) 676 { 677 GError* err = null; 678 679 auto retStr = g_regex_replace_eval(gRegex, Str.toStringz(string_), cast(ptrdiff_t)string_.length, startPosition, matchOptions, eval, userData, &err); 680 681 if (err !is null) 682 { 683 throw new GException( new ErrorG(err) ); 684 } 685 686 scope(exit) Str.freeString(retStr); 687 return Str.toString(retStr); 688 } 689 690 /** 691 * Replaces all occurrences of the pattern in @regex with the 692 * replacement text. @replacement is replaced literally, to 693 * include backreferences use g_regex_replace(). 694 * 695 * Setting @start_position differs from just passing over a 696 * shortened string and setting #G_REGEX_MATCH_NOTBOL in the 697 * case of a pattern that begins with any kind of lookbehind 698 * assertion, such as "\b". 699 * 700 * Params: 701 * string_ = the string to perform matches against 702 * startPosition = starting index of the string to match, in bytes 703 * replacement = text to replace each match with 704 * matchOptions = options for the match 705 * 706 * Returns: a newly allocated string containing the replacements 707 * 708 * Since: 2.14 709 * 710 * Throws: GException on failure. 711 */ 712 public string replaceLiteral(string string_, int startPosition, string replacement, GRegexMatchFlags matchOptions) 713 { 714 GError* err = null; 715 716 auto retStr = g_regex_replace_literal(gRegex, Str.toStringz(string_), cast(ptrdiff_t)string_.length, startPosition, Str.toStringz(replacement), matchOptions, &err); 717 718 if (err !is null) 719 { 720 throw new GException( new ErrorG(err) ); 721 } 722 723 scope(exit) Str.freeString(retStr); 724 return Str.toString(retStr); 725 } 726 727 /** 728 * Breaks the string on the pattern, and returns an array of the tokens. 729 * If the pattern contains capturing parentheses, then the text for each 730 * of the substrings will also be returned. If the pattern does not match 731 * anywhere in the string, then the whole string is returned as the first 732 * token. 733 * 734 * As a special case, the result of splitting the empty string "" is an 735 * empty vector, not a vector containing a single string. The reason for 736 * this special case is that being able to represent an empty vector is 737 * typically more useful than consistent handling of empty elements. If 738 * you do need to represent empty elements, you'll need to check for the 739 * empty string before calling this function. 740 * 741 * A pattern that can match empty strings splits @string into separate 742 * characters wherever it matches the empty string between characters. 743 * For example splitting "ab c" using as a separator "\s*", you will get 744 * "a", "b" and "c". 745 * 746 * Params: 747 * string_ = the string to split with the pattern 748 * matchOptions = match time option flags 749 * 750 * Returns: a %NULL-terminated gchar ** array. Free 751 * it using g_strfreev() 752 * 753 * Since: 2.14 754 */ 755 public string[] split(string string_, GRegexMatchFlags matchOptions) 756 { 757 auto retStr = g_regex_split(gRegex, Str.toStringz(string_), matchOptions); 758 759 scope(exit) Str.freeStringArray(retStr); 760 return Str.toStringArray(retStr); 761 } 762 763 /** 764 * Breaks the string on the pattern, and returns an array of the tokens. 765 * If the pattern contains capturing parentheses, then the text for each 766 * of the substrings will also be returned. If the pattern does not match 767 * anywhere in the string, then the whole string is returned as the first 768 * token. 769 * 770 * As a special case, the result of splitting the empty string "" is an 771 * empty vector, not a vector containing a single string. The reason for 772 * this special case is that being able to represent an empty vector is 773 * typically more useful than consistent handling of empty elements. If 774 * you do need to represent empty elements, you'll need to check for the 775 * empty string before calling this function. 776 * 777 * A pattern that can match empty strings splits @string into separate 778 * characters wherever it matches the empty string between characters. 779 * For example splitting "ab c" using as a separator "\s*", you will get 780 * "a", "b" and "c". 781 * 782 * Setting @start_position differs from just passing over a shortened 783 * string and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern 784 * that begins with any kind of lookbehind assertion, such as "\b". 785 * 786 * Params: 787 * string_ = the string to split with the pattern 788 * startPosition = starting index of the string to match, in bytes 789 * matchOptions = match time option flags 790 * maxTokens = the maximum number of tokens to split @string into. 791 * If this is less than 1, the string is split completely 792 * 793 * Returns: a %NULL-terminated gchar ** array. Free 794 * it using g_strfreev() 795 * 796 * Since: 2.14 797 * 798 * Throws: GException on failure. 799 */ 800 public string[] splitFull(string string_, int startPosition, GRegexMatchFlags matchOptions, int maxTokens) 801 { 802 GError* err = null; 803 804 auto retStr = g_regex_split_full(gRegex, Str.toStringz(string_), cast(ptrdiff_t)string_.length, startPosition, matchOptions, maxTokens, &err); 805 806 if (err !is null) 807 { 808 throw new GException( new ErrorG(err) ); 809 } 810 811 scope(exit) Str.freeStringArray(retStr); 812 return Str.toStringArray(retStr); 813 } 814 815 /** 816 * Decreases reference count of @regex by 1. When reference count drops 817 * to zero, it frees all the memory associated with the regex structure. 818 * 819 * Since: 2.14 820 */ 821 public void unref() 822 { 823 g_regex_unref(gRegex); 824 } 825 826 /** 827 * Checks whether @replacement is a valid replacement string 828 * (see g_regex_replace()), i.e. that all escape sequences in 829 * it are valid. 830 * 831 * If @has_references is not %NULL then @replacement is checked 832 * for pattern references. For instance, replacement text 'foo\n' 833 * does not contain references and may be evaluated without information 834 * about actual match, but '\0\1' (whole match followed by first 835 * subpattern) requires valid #GMatchInfo object. 836 * 837 * Params: 838 * replacement = the replacement string 839 * hasReferences = location to store information about 840 * references in @replacement or %NULL 841 * 842 * Returns: whether @replacement is a valid replacement string 843 * 844 * Since: 2.14 845 * 846 * Throws: GException on failure. 847 */ 848 public static bool checkReplacement(string replacement, out bool hasReferences) 849 { 850 int outhasReferences; 851 GError* err = null; 852 853 auto __p = g_regex_check_replacement(Str.toStringz(replacement), &outhasReferences, &err) != 0; 854 855 if (err !is null) 856 { 857 throw new GException( new ErrorG(err) ); 858 } 859 860 hasReferences = (outhasReferences == 1); 861 862 return __p; 863 } 864 865 /** */ 866 public static GQuark errorQuark() 867 { 868 return g_regex_error_quark(); 869 } 870 871 /** 872 * Escapes the nul characters in @string to "\x00". It can be used 873 * to compile a regex with embedded nul characters. 874 * 875 * For completeness, @length can be -1 for a nul-terminated string. 876 * In this case the output string will be of course equal to @string. 877 * 878 * Params: 879 * string_ = the string to escape 880 * length = the length of @string 881 * 882 * Returns: a newly-allocated escaped string 883 * 884 * Since: 2.30 885 */ 886 public static string escapeNul(string string_, int length) 887 { 888 auto retStr = g_regex_escape_nul(Str.toStringz(string_), length); 889 890 scope(exit) Str.freeString(retStr); 891 return Str.toString(retStr); 892 } 893 894 /** 895 * Escapes the special characters used for regular expressions 896 * in @string, for instance "a.b*c" becomes "a\.b\*c". This 897 * function is useful to dynamically generate regular expressions. 898 * 899 * @string can contain nul characters that are replaced with "\0", 900 * in this case remember to specify the correct length of @string 901 * in @length. 902 * 903 * Params: 904 * string_ = the string to escape 905 * 906 * Returns: a newly-allocated escaped string 907 * 908 * Since: 2.14 909 */ 910 public static string escapeString(string string_) 911 { 912 auto retStr = g_regex_escape_string(Str.toStringz(string_), cast(int)string_.length); 913 914 scope(exit) Str.freeString(retStr); 915 return Str.toString(retStr); 916 } 917 918 /** 919 * Scans for a match in @string for @pattern. 920 * 921 * This function is equivalent to g_regex_match() but it does not 922 * require to compile the pattern with g_regex_new(), avoiding some 923 * lines of code when you need just to do a match without extracting 924 * substrings, capture counts, and so on. 925 * 926 * If this function is to be called on the same @pattern more than 927 * once, it's more efficient to compile the pattern once with 928 * g_regex_new() and then use g_regex_match(). 929 * 930 * Params: 931 * pattern = the regular expression 932 * string_ = the string to scan for matches 933 * compileOptions = compile options for the regular expression, or 0 934 * matchOptions = match options, or 0 935 * 936 * Returns: %TRUE if the string matched, %FALSE otherwise 937 * 938 * Since: 2.14 939 */ 940 public static bool matchSimple(string pattern, string string_, GRegexCompileFlags compileOptions, GRegexMatchFlags matchOptions) 941 { 942 return g_regex_match_simple(Str.toStringz(pattern), Str.toStringz(string_), compileOptions, matchOptions) != 0; 943 } 944 945 /** 946 * Breaks the string on the pattern, and returns an array of 947 * the tokens. If the pattern contains capturing parentheses, 948 * then the text for each of the substrings will also be returned. 949 * If the pattern does not match anywhere in the string, then the 950 * whole string is returned as the first token. 951 * 952 * This function is equivalent to g_regex_split() but it does 953 * not require to compile the pattern with g_regex_new(), avoiding 954 * some lines of code when you need just to do a split without 955 * extracting substrings, capture counts, and so on. 956 * 957 * If this function is to be called on the same @pattern more than 958 * once, it's more efficient to compile the pattern once with 959 * g_regex_new() and then use g_regex_split(). 960 * 961 * As a special case, the result of splitting the empty string "" 962 * is an empty vector, not a vector containing a single string. 963 * The reason for this special case is that being able to represent 964 * an empty vector is typically more useful than consistent handling 965 * of empty elements. If you do need to represent empty elements, 966 * you'll need to check for the empty string before calling this 967 * function. 968 * 969 * A pattern that can match empty strings splits @string into 970 * separate characters wherever it matches the empty string between 971 * characters. For example splitting "ab c" using as a separator 972 * "\s*", you will get "a", "b" and "c". 973 * 974 * Params: 975 * pattern = the regular expression 976 * string_ = the string to scan for matches 977 * compileOptions = compile options for the regular expression, or 0 978 * matchOptions = match options, or 0 979 * 980 * Returns: a %NULL-terminated array of strings. Free 981 * it using g_strfreev() 982 * 983 * Since: 2.14 984 */ 985 public static string[] splitSimple(string pattern, string string_, GRegexCompileFlags compileOptions, GRegexMatchFlags matchOptions) 986 { 987 auto retStr = g_regex_split_simple(Str.toStringz(pattern), Str.toStringz(string_), compileOptions, matchOptions); 988 989 scope(exit) Str.freeStringArray(retStr); 990 return Str.toStringArray(retStr); 991 } 992 }