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