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 * A #GMatchInfo structure, used to get information on the match, 295 * is stored in @match_info if not %NULL. Note that if @match_info 296 * is not %NULL then it is created even if the function returns %FALSE, 297 * i.e. you must free it regardless if regular expression actually matched. 298 * 299 * To retrieve all the non-overlapping matches of the pattern in 300 * string you can use g_match_info_next(). 301 * 302 * |[<!-- language="C" --> 303 * static void 304 * print_uppercase_words (const gchar *string) 305 * { 306 * // Print all uppercase-only words. 307 * GRegex *regex; 308 * GMatchInfo *match_info; 309 * 310 * regex = g_regex_new ("[A-Z]+", 0, 0, NULL); 311 * g_regex_match (regex, string, 0, &match_info); 312 * while (g_match_info_matches (match_info)) 313 * { 314 * gchar *word = g_match_info_fetch (match_info, 0); 315 * g_print ("Found: %s\n", word); 316 * g_free (word); 317 * g_match_info_next (match_info, NULL); 318 * } 319 * g_match_info_free (match_info); 320 * g_regex_unref (regex); 321 * } 322 * ]| 323 * 324 * @string is not copied and is used in #GMatchInfo internally. If 325 * you use any #GMatchInfo method (except g_match_info_free()) after 326 * freeing or modifying @string then the behaviour is undefined. 327 * 328 * Params: 329 * string_ = the string to scan for matches 330 * matchOptions = match options 331 * matchInfo = pointer to location where to store 332 * the #GMatchInfo, or %NULL if you do not need it 333 * 334 * Returns: %TRUE is the string matched, %FALSE otherwise 335 * 336 * Since: 2.14 337 */ 338 public bool match(string string_, GRegexMatchFlags matchOptions, out MatchInfo matchInfo) 339 { 340 GMatchInfo* outmatchInfo = null; 341 342 auto p = g_regex_match(gRegex, Str.toStringz(string_), matchOptions, &outmatchInfo) != 0; 343 344 matchInfo = new MatchInfo(outmatchInfo); 345 346 return p; 347 } 348 349 /** 350 * Using the standard algorithm for regular expression matching only 351 * the longest match in the string is retrieved. This function uses 352 * a different algorithm so it can retrieve all the possible matches. 353 * For more documentation see g_regex_match_all_full(). 354 * 355 * A #GMatchInfo structure, used to get information on the match, is 356 * stored in @match_info if not %NULL. Note that if @match_info is 357 * not %NULL then it is created even if the function returns %FALSE, 358 * i.e. you must free it regardless if regular expression actually 359 * matched. 360 * 361 * @string is not copied and is used in #GMatchInfo internally. If 362 * you use any #GMatchInfo method (except g_match_info_free()) after 363 * freeing or modifying @string then the behaviour is undefined. 364 * 365 * Params: 366 * string_ = the string to scan for matches 367 * matchOptions = match options 368 * matchInfo = pointer to location where to store 369 * the #GMatchInfo, or %NULL if you do not need it 370 * 371 * Returns: %TRUE is the string matched, %FALSE otherwise 372 * 373 * Since: 2.14 374 */ 375 public bool matchAll(string string_, GRegexMatchFlags matchOptions, out MatchInfo matchInfo) 376 { 377 GMatchInfo* outmatchInfo = null; 378 379 auto p = g_regex_match_all(gRegex, Str.toStringz(string_), matchOptions, &outmatchInfo) != 0; 380 381 matchInfo = new MatchInfo(outmatchInfo); 382 383 return p; 384 } 385 386 /** 387 * Using the standard algorithm for regular expression matching only 388 * the longest match in the string is retrieved, it is not possible 389 * to obtain all the available matches. For instance matching 390 * "<a> <b> <c>" against the pattern "<.*>" 391 * you get "<a> <b> <c>". 392 * 393 * This function uses a different algorithm (called DFA, i.e. deterministic 394 * finite automaton), so it can retrieve all the possible matches, all 395 * starting at the same point in the string. For instance matching 396 * "<a> <b> <c>" against the pattern "<.*>;" 397 * you would obtain three matches: "<a> <b> <c>", 398 * "<a> <b>" and "<a>". 399 * 400 * The number of matched strings is retrieved using 401 * g_match_info_get_match_count(). To obtain the matched strings and 402 * their position you can use, respectively, g_match_info_fetch() and 403 * g_match_info_fetch_pos(). Note that the strings are returned in 404 * reverse order of length; that is, the longest matching string is 405 * given first. 406 * 407 * Note that the DFA algorithm is slower than the standard one and it 408 * is not able to capture substrings, so backreferences do not work. 409 * 410 * Setting @start_position differs from just passing over a shortened 411 * string and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern 412 * that begins with any kind of lookbehind assertion, such as "\b". 413 * 414 * A #GMatchInfo structure, used to get information on the match, is 415 * stored in @match_info if not %NULL. Note that if @match_info is 416 * not %NULL then it is created even if the function returns %FALSE, 417 * i.e. you must free it regardless if regular expression actually 418 * matched. 419 * 420 * @string is not copied and is used in #GMatchInfo internally. If 421 * you use any #GMatchInfo method (except g_match_info_free()) after 422 * freeing or modifying @string then the behaviour is undefined. 423 * 424 * Params: 425 * string_ = the string to scan for matches 426 * startPosition = starting index of the string to match, in bytes 427 * matchOptions = match options 428 * matchInfo = pointer to location where to store 429 * the #GMatchInfo, or %NULL if you do not need it 430 * 431 * Returns: %TRUE is the string matched, %FALSE otherwise 432 * 433 * Since: 2.14 434 * 435 * Throws: GException on failure. 436 */ 437 public bool matchAllFull(string string_, int startPosition, GRegexMatchFlags matchOptions, out MatchInfo matchInfo) 438 { 439 GMatchInfo* outmatchInfo = null; 440 GError* err = null; 441 442 auto p = g_regex_match_all_full(gRegex, Str.toStringz(string_), cast(ptrdiff_t)string_.length, startPosition, matchOptions, &outmatchInfo, &err) != 0; 443 444 if (err !is null) 445 { 446 throw new GException( new ErrorG(err) ); 447 } 448 449 matchInfo = new MatchInfo(outmatchInfo); 450 451 return p; 452 } 453 454 /** 455 * Scans for a match in string for the pattern in @regex. 456 * The @match_options are combined with the match options specified 457 * when the @regex structure was created, letting you have more 458 * flexibility in reusing #GRegex structures. 459 * 460 * Setting @start_position differs from just passing over a shortened 461 * string and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern 462 * that begins with any kind of lookbehind assertion, such as "\b". 463 * 464 * A #GMatchInfo structure, used to get information on the match, is 465 * stored in @match_info if not %NULL. Note that if @match_info is 466 * not %NULL then it is created even if the function returns %FALSE, 467 * i.e. you must free it regardless if regular expression actually 468 * matched. 469 * 470 * @string is not copied and is used in #GMatchInfo internally. If 471 * you use any #GMatchInfo method (except g_match_info_free()) after 472 * freeing or modifying @string then the behaviour is undefined. 473 * 474 * To retrieve all the non-overlapping matches of the pattern in 475 * string you can use g_match_info_next(). 476 * 477 * |[<!-- language="C" --> 478 * static void 479 * print_uppercase_words (const gchar *string) 480 * { 481 * // Print all uppercase-only words. 482 * GRegex *regex; 483 * GMatchInfo *match_info; 484 * GError *error = NULL; 485 * 486 * regex = g_regex_new ("[A-Z]+", 0, 0, NULL); 487 * g_regex_match_full (regex, string, -1, 0, 0, &match_info, &error); 488 * while (g_match_info_matches (match_info)) 489 * { 490 * gchar *word = g_match_info_fetch (match_info, 0); 491 * g_print ("Found: %s\n", word); 492 * g_free (word); 493 * g_match_info_next (match_info, &error); 494 * } 495 * g_match_info_free (match_info); 496 * g_regex_unref (regex); 497 * if (error != NULL) 498 * { 499 * g_printerr ("Error while matching: %s\n", error->message); 500 * g_error_free (error); 501 * } 502 * } 503 * ]| 504 * 505 * Params: 506 * string_ = the string to scan for matches 507 * startPosition = starting index of the string to match, in bytes 508 * matchOptions = match options 509 * matchInfo = pointer to location where to store 510 * the #GMatchInfo, or %NULL if you do not need it 511 * 512 * Returns: %TRUE is the string matched, %FALSE otherwise 513 * 514 * Since: 2.14 515 * 516 * Throws: GException on failure. 517 */ 518 public bool matchFull(string string_, int startPosition, GRegexMatchFlags matchOptions, out MatchInfo matchInfo) 519 { 520 GMatchInfo* outmatchInfo = null; 521 GError* err = null; 522 523 auto p = g_regex_match_full(gRegex, Str.toStringz(string_), cast(ptrdiff_t)string_.length, startPosition, matchOptions, &outmatchInfo, &err) != 0; 524 525 if (err !is null) 526 { 527 throw new GException( new ErrorG(err) ); 528 } 529 530 matchInfo = new MatchInfo(outmatchInfo); 531 532 return p; 533 } 534 535 alias doref = ref_; 536 /** 537 * Increases reference count of @regex by 1. 538 * 539 * Returns: @regex 540 * 541 * Since: 2.14 542 */ 543 public Regex ref_() 544 { 545 auto p = g_regex_ref(gRegex); 546 547 if(p is null) 548 { 549 return null; 550 } 551 552 return new Regex(cast(GRegex*) p, true); 553 } 554 555 /** 556 * Replaces all occurrences of the pattern in @regex with the 557 * replacement text. Backreferences of the form '\number' or 558 * '\g<number>' in the replacement text are interpolated by the 559 * number-th captured subexpression of the match, '\g<name>' refers 560 * to the captured subexpression with the given name. '\0' refers 561 * to the complete match, but '\0' followed by a number is the octal 562 * representation of a character. To include a literal '\' in the 563 * replacement, write '\\\\'. 564 * 565 * There are also escapes that changes the case of the following text: 566 * 567 * - \l: Convert to lower case the next character 568 * - \u: Convert to upper case the next character 569 * - \L: Convert to lower case till \E 570 * - \U: Convert to upper case till \E 571 * - \E: End case modification 572 * 573 * If you do not need to use backreferences use g_regex_replace_literal(). 574 * 575 * The @replacement string must be UTF-8 encoded even if #G_REGEX_RAW was 576 * passed to g_regex_new(). If you want to use not UTF-8 encoded stings 577 * you can use g_regex_replace_literal(). 578 * 579 * Setting @start_position differs from just passing over a shortened 580 * string and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern that 581 * begins with any kind of lookbehind assertion, such as "\b". 582 * 583 * Params: 584 * string_ = the string to perform matches against 585 * startPosition = starting index of the string to match, in bytes 586 * replacement = text to replace each match with 587 * matchOptions = options for the match 588 * 589 * Returns: a newly allocated string containing the replacements 590 * 591 * Since: 2.14 592 * 593 * Throws: GException on failure. 594 */ 595 public string replace(string string_, int startPosition, string replacement, GRegexMatchFlags matchOptions) 596 { 597 GError* err = null; 598 599 auto retStr = g_regex_replace(gRegex, Str.toStringz(string_), cast(ptrdiff_t)string_.length, startPosition, Str.toStringz(replacement), matchOptions, &err); 600 601 if (err !is null) 602 { 603 throw new GException( new ErrorG(err) ); 604 } 605 606 scope(exit) Str.freeString(retStr); 607 return Str.toString(retStr); 608 } 609 610 /** 611 * Replaces occurrences of the pattern in regex with the output of 612 * @eval for that occurrence. 613 * 614 * Setting @start_position differs from just passing over a shortened 615 * string and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern 616 * that begins with any kind of lookbehind assertion, such as "\b". 617 * 618 * The following example uses g_regex_replace_eval() to replace multiple 619 * strings at once: 620 * |[<!-- language="C" --> 621 * static gboolean 622 * eval_cb (const GMatchInfo *info, 623 * GString *res, 624 * gpointer data) 625 * { 626 * gchar *match; 627 * gchar *r; 628 * 629 * match = g_match_info_fetch (info, 0); 630 * r = g_hash_table_lookup ((GHashTable *)data, match); 631 * g_string_append (res, r); 632 * g_free (match); 633 * 634 * return FALSE; 635 * } 636 * 637 * ... 638 * 639 * GRegex *reg; 640 * GHashTable *h; 641 * gchar *res; 642 * 643 * h = g_hash_table_new (g_str_hash, g_str_equal); 644 * 645 * g_hash_table_insert (h, "1", "ONE"); 646 * g_hash_table_insert (h, "2", "TWO"); 647 * g_hash_table_insert (h, "3", "THREE"); 648 * g_hash_table_insert (h, "4", "FOUR"); 649 * 650 * reg = g_regex_new ("1|2|3|4", 0, 0, NULL); 651 * res = g_regex_replace_eval (reg, text, -1, 0, 0, eval_cb, h, NULL); 652 * g_hash_table_destroy (h); 653 * 654 * ... 655 * ]| 656 * 657 * Params: 658 * string_ = string to perform matches against 659 * startPosition = starting index of the string to match, in bytes 660 * matchOptions = options for the match 661 * eval = a function to call for each match 662 * userData = user data to pass to the function 663 * 664 * Returns: a newly allocated string containing the replacements 665 * 666 * Since: 2.14 667 * 668 * Throws: GException on failure. 669 */ 670 public string replaceEval(string string_, int startPosition, GRegexMatchFlags matchOptions, GRegexEvalCallback eval, void* userData) 671 { 672 GError* err = null; 673 674 auto retStr = g_regex_replace_eval(gRegex, Str.toStringz(string_), cast(ptrdiff_t)string_.length, startPosition, matchOptions, eval, userData, &err); 675 676 if (err !is null) 677 { 678 throw new GException( new ErrorG(err) ); 679 } 680 681 scope(exit) Str.freeString(retStr); 682 return Str.toString(retStr); 683 } 684 685 /** 686 * Replaces all occurrences of the pattern in @regex with the 687 * replacement text. @replacement is replaced literally, to 688 * include backreferences use g_regex_replace(). 689 * 690 * Setting @start_position differs from just passing over a 691 * shortened string and setting #G_REGEX_MATCH_NOTBOL in the 692 * case of a pattern that begins with any kind of lookbehind 693 * assertion, such as "\b". 694 * 695 * Params: 696 * string_ = the string to perform matches against 697 * startPosition = starting index of the string to match, in bytes 698 * replacement = text to replace each match with 699 * matchOptions = options for the match 700 * 701 * Returns: a newly allocated string containing the replacements 702 * 703 * Since: 2.14 704 * 705 * Throws: GException on failure. 706 */ 707 public string replaceLiteral(string string_, int startPosition, string replacement, GRegexMatchFlags matchOptions) 708 { 709 GError* err = null; 710 711 auto retStr = g_regex_replace_literal(gRegex, Str.toStringz(string_), cast(ptrdiff_t)string_.length, startPosition, Str.toStringz(replacement), matchOptions, &err); 712 713 if (err !is null) 714 { 715 throw new GException( new ErrorG(err) ); 716 } 717 718 scope(exit) Str.freeString(retStr); 719 return Str.toString(retStr); 720 } 721 722 /** 723 * Breaks the string on the pattern, and returns an array of the tokens. 724 * If the pattern contains capturing parentheses, then the text for each 725 * of the substrings will also be returned. If the pattern does not match 726 * anywhere in the string, then the whole string is returned as the first 727 * token. 728 * 729 * As a special case, the result of splitting the empty string "" is an 730 * empty vector, not a vector containing a single string. The reason for 731 * this special case is that being able to represent a empty vector is 732 * typically more useful than consistent handling of empty elements. If 733 * you do need to represent empty elements, you'll need to check for the 734 * empty string before calling this function. 735 * 736 * A pattern that can match empty strings splits @string into separate 737 * characters wherever it matches the empty string between characters. 738 * For example splitting "ab c" using as a separator "\s*", you will get 739 * "a", "b" and "c". 740 * 741 * Params: 742 * string_ = the string to split with the pattern 743 * matchOptions = match time option flags 744 * 745 * Returns: a %NULL-terminated gchar ** array. Free 746 * it using g_strfreev() 747 * 748 * Since: 2.14 749 */ 750 public string[] split(string string_, GRegexMatchFlags matchOptions) 751 { 752 auto retStr = g_regex_split(gRegex, Str.toStringz(string_), matchOptions); 753 754 scope(exit) Str.freeStringArray(retStr); 755 return Str.toStringArray(retStr); 756 } 757 758 /** 759 * Breaks the string on the pattern, and returns an array of the tokens. 760 * If the pattern contains capturing parentheses, then the text for each 761 * of the substrings will also be returned. If the pattern does not match 762 * anywhere in the string, then the whole string is returned as the first 763 * token. 764 * 765 * As a special case, the result of splitting the empty string "" is an 766 * empty vector, not a vector containing a single string. The reason for 767 * this special case is that being able to represent a empty vector is 768 * typically more useful than consistent handling of empty elements. If 769 * you do need to represent empty elements, you'll need to check for the 770 * empty string before calling this function. 771 * 772 * A pattern that can match empty strings splits @string into separate 773 * characters wherever it matches the empty string between characters. 774 * For example splitting "ab c" using as a separator "\s*", you will get 775 * "a", "b" and "c". 776 * 777 * Setting @start_position differs from just passing over a shortened 778 * string and setting #G_REGEX_MATCH_NOTBOL in the case of a pattern 779 * that begins with any kind of lookbehind assertion, such as "\b". 780 * 781 * Params: 782 * string_ = the string to split with the pattern 783 * startPosition = starting index of the string to match, in bytes 784 * matchOptions = match time option flags 785 * maxTokens = the maximum number of tokens to split @string into. 786 * If this is less than 1, the string is split completely 787 * 788 * Returns: a %NULL-terminated gchar ** array. Free 789 * it using g_strfreev() 790 * 791 * Since: 2.14 792 * 793 * Throws: GException on failure. 794 */ 795 public string[] splitFull(string string_, int startPosition, GRegexMatchFlags matchOptions, int maxTokens) 796 { 797 GError* err = null; 798 799 auto retStr = g_regex_split_full(gRegex, Str.toStringz(string_), cast(ptrdiff_t)string_.length, startPosition, matchOptions, maxTokens, &err); 800 801 if (err !is null) 802 { 803 throw new GException( new ErrorG(err) ); 804 } 805 806 scope(exit) Str.freeStringArray(retStr); 807 return Str.toStringArray(retStr); 808 } 809 810 /** 811 * Decreases reference count of @regex by 1. When reference count drops 812 * to zero, it frees all the memory associated with the regex structure. 813 * 814 * Since: 2.14 815 */ 816 public void unref() 817 { 818 g_regex_unref(gRegex); 819 } 820 821 /** 822 * Checks whether @replacement is a valid replacement string 823 * (see g_regex_replace()), i.e. that all escape sequences in 824 * it are valid. 825 * 826 * If @has_references is not %NULL then @replacement is checked 827 * for pattern references. For instance, replacement text 'foo\n' 828 * does not contain references and may be evaluated without information 829 * about actual match, but '\0\1' (whole match followed by first 830 * subpattern) requires valid #GMatchInfo object. 831 * 832 * Params: 833 * replacement = the replacement string 834 * hasReferences = location to store information about 835 * references in @replacement or %NULL 836 * 837 * Returns: whether @replacement is a valid replacement string 838 * 839 * Since: 2.14 840 * 841 * Throws: GException on failure. 842 */ 843 public static bool checkReplacement(string replacement, out bool hasReferences) 844 { 845 int outhasReferences; 846 GError* err = null; 847 848 auto p = g_regex_check_replacement(Str.toStringz(replacement), &outhasReferences, &err) != 0; 849 850 if (err !is null) 851 { 852 throw new GException( new ErrorG(err) ); 853 } 854 855 hasReferences = (outhasReferences == 1); 856 857 return p; 858 } 859 860 /** */ 861 public static GQuark errorQuark() 862 { 863 return g_regex_error_quark(); 864 } 865 866 /** 867 * Escapes the nul characters in @string to "\x00". It can be used 868 * to compile a regex with embedded nul characters. 869 * 870 * For completeness, @length can be -1 for a nul-terminated string. 871 * In this case the output string will be of course equal to @string. 872 * 873 * Params: 874 * string_ = the string to escape 875 * length = the length of @string 876 * 877 * Returns: a newly-allocated escaped string 878 * 879 * Since: 2.30 880 */ 881 public static string escapeNul(string string_, int length) 882 { 883 auto retStr = g_regex_escape_nul(Str.toStringz(string_), length); 884 885 scope(exit) Str.freeString(retStr); 886 return Str.toString(retStr); 887 } 888 889 /** 890 * Escapes the special characters used for regular expressions 891 * in @string, for instance "a.b*c" becomes "a\.b\*c". This 892 * function is useful to dynamically generate regular expressions. 893 * 894 * @string can contain nul characters that are replaced with "\0", 895 * in this case remember to specify the correct length of @string 896 * in @length. 897 * 898 * Params: 899 * string_ = the string to escape 900 * 901 * Returns: a newly-allocated escaped string 902 * 903 * Since: 2.14 904 */ 905 public static string escapeString(string string_) 906 { 907 auto retStr = g_regex_escape_string(Str.toStringz(string_), cast(int)string_.length); 908 909 scope(exit) Str.freeString(retStr); 910 return Str.toString(retStr); 911 } 912 913 /** 914 * Scans for a match in @string for @pattern. 915 * 916 * This function is equivalent to g_regex_match() but it does not 917 * require to compile the pattern with g_regex_new(), avoiding some 918 * lines of code when you need just to do a match without extracting 919 * substrings, capture counts, and so on. 920 * 921 * If this function is to be called on the same @pattern more than 922 * once, it's more efficient to compile the pattern once with 923 * g_regex_new() and then use g_regex_match(). 924 * 925 * Params: 926 * pattern = the regular expression 927 * string_ = the string to scan for matches 928 * compileOptions = compile options for the regular expression, or 0 929 * matchOptions = match options, or 0 930 * 931 * Returns: %TRUE if the string matched, %FALSE otherwise 932 * 933 * Since: 2.14 934 */ 935 public static bool matchSimple(string pattern, string string_, GRegexCompileFlags compileOptions, GRegexMatchFlags matchOptions) 936 { 937 return g_regex_match_simple(Str.toStringz(pattern), Str.toStringz(string_), compileOptions, matchOptions) != 0; 938 } 939 940 /** 941 * Breaks the string on the pattern, and returns an array of 942 * the tokens. If the pattern contains capturing parentheses, 943 * then the text for each of the substrings will also be returned. 944 * If the pattern does not match anywhere in the string, then the 945 * whole string is returned as the first token. 946 * 947 * This function is equivalent to g_regex_split() but it does 948 * not require to compile the pattern with g_regex_new(), avoiding 949 * some lines of code when you need just to do a split without 950 * extracting substrings, capture counts, and so on. 951 * 952 * If this function is to be called on the same @pattern more than 953 * once, it's more efficient to compile the pattern once with 954 * g_regex_new() and then use g_regex_split(). 955 * 956 * As a special case, the result of splitting the empty string "" 957 * is an empty vector, not a vector containing a single string. 958 * The reason for this special case is that being able to represent 959 * a empty vector is typically more useful than consistent handling 960 * of empty elements. If you do need to represent empty elements, 961 * you'll need to check for the empty string before calling this 962 * function. 963 * 964 * A pattern that can match empty strings splits @string into 965 * separate characters wherever it matches the empty string between 966 * characters. For example splitting "ab c" using as a separator 967 * "\s*", you will get "a", "b" and "c". 968 * 969 * Params: 970 * pattern = the regular expression 971 * string_ = the string to scan for matches 972 * compileOptions = compile options for the regular expression, or 0 973 * matchOptions = match options, or 0 974 * 975 * Returns: a %NULL-terminated array of strings. Free 976 * it using g_strfreev() 977 * 978 * Since: 2.14 979 */ 980 public static string[] splitSimple(string pattern, string string_, GRegexCompileFlags compileOptions, GRegexMatchFlags matchOptions) 981 { 982 auto retStr = g_regex_split_simple(Str.toStringz(pattern), Str.toStringz(string_), compileOptions, matchOptions); 983 984 scope(exit) Str.freeStringArray(retStr); 985 return Str.toStringArray(retStr); 986 } 987 }