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