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