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