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