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